ASIHTTP: addOperation when other threads are running - iphone

I have a project using ASIHTTP to multi download files from a site
when I add a new request:
[networkQueue cancelAllOperations];
[networkQueue setDownloadProgressDelegate:a];
[networkQueue setDelegate:self];
[networkQueue setRequestDidFinishSelector:#selector(requestDone:)];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[networkQueue addOperation:request];
[networkQueue go];
it reported:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[ASINetworkQueue addOperation:]: operation is executing and cannot be enqueued'
It looks like I can not add a new request when others are running.
Welcome any comment
Thanks
interdev

If you are using a network queue, you cannot start the operation before you enqueue it. Don't call startAsynchrnous, just enqueue the operation and the network queue will start it when it dequeues it. Pretty much exactly what your error message says ;)

just remove the [request startAsynchronous]; and it will work fine for you.
hope this helps.
Thanks.

Related

What are the working of ASIFormDataRequest and ASINetworkQueue in Iphone

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://trade2rise.com/project/dry/windex.php?itfpage=register"]];
ASINetworkQueue *networkQueue = [[ASINetworkQueue alloc] init];
[request setPostValue:txt_name.text forKey:#"name"];
[request setPostValue:txt_con_number.text forKey:#"phone"];
[request setPostValue:txt_email.text forKey:#"email"];
[request setPostValue:txt_pwd.text forKey:#"password"];
[request setPostValue:txt_con_pwd.text forKey:#"password2"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[networkQueue addOperation:request];
[networkQueue go];
I am new for the Iphone Please explain about abodve term?
If you known the http post method , you will known the key and value pair at the post form. so the code above just like add relative key and value to the form, the add the queue to post request.
ASIFormDataRequest *request // the url bind request,just form post form
ASINetworkQueue *networkQueue // the work queue to maintain the request in a queue (FIFO)

ASIDownLoadCache doesn't work

ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:url]];
request.requestHeaders = header;
request.requestMethod = #"GET";
request.tag = DBRequestTypeChannelCategory;
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setSecondsToCache:60*60*24*3];
[request startAsynchronous];
this is my code about http
and if i turn my phone to fly model.
i got this
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x1fd67bf0 {NSUnderlyingError=0x1fd66bf0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 2.)", NSLocalizedDescription=A connection failure occurred}
The error you've posted means that the crash was from a connection failure, which makes sense if you have airplane mode enabled. You should be able to eliminate this be setting a failure handler.
[request setFailedBlock:void^{
//
}];
Or
[request setDidFailSelector:#selector(requestWentWrong:)];
To access cached data without internet access simply add the following to your request.
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
In order for this to work you need to make sure that cache storage is set to permanent to prevent the caches from being removed when the user exists the app.
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

Getting SIGABRT with ASINetworkQueue due to Threading

I've been getting SIGABRT when using ASINetworkQueue, and I've a suspicion that its related to threading and I'm not sure what or why. If I comment out the following code everything works, but if I leave it in, the app will just stop with a SIGABRT and no other information.
// create ASINetwork queue
if (networkQueue == nil)
{
networkQueue = [ASINetworkQueue queue];
[networkQueue setDelegate:self];
[networkQueue setRequestDidFinishSelector:#selector(requestFinished:)];
[networkQueue setRequestDidFailSelector:#selector(requestFailed:)];
[networkQueue setQueueDidFinishSelector:#selector(queueFinished:)];
[networkQueue retain];
}
// if branding url available
if ([brandingURL length] > 0) {
NSString *stringURL = [NSString stringWithFormat:#"%#/admin%#", SERVER_BASE_URL, brandingURL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:stringURL]];
[request setUserInfo:[NSDictionary dictionaryWithObject:#"branding" forKey:#"requestType"]];
[networkQueue addOperation:request];
}
[networkQueue go];
The thread listing is in the following image gallery which is the only thing which might shed some light on whats going on with the threads.
http://imgur.com/a/msp8x
Any advice or suggestions much welcome.

Resuming canceled ASIHTTPRequest

Is it possible to resume canceled download in ASIHTTPRequest? I download file with this code:
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.download",fileName]]];
[request setDidFailSelector:#selector(requestDidFailed:)];
[request setDidStartSelector:#selector(requestDidStarted:)];
[request setDidFailSelector:#selector(requestDidFinished:)];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadDestinationPath:path];
[request setShowAccurateProgress:YES];
[request startAsynchronous];
And cancel with this code:
[request cancel];
How to resume this download if it's possible? Or how to pause requests correctly?
This site might help, scroll down to the Resuming interrupted downloads section. http://allseeing-i.com/ASIHTTPRequest/How-to-use

ASIHTTPRequest: Receive delegates from several requests within a network queue

I use ASINetworkQueue to send two requests, which are in a queue.
My problem is, that I'm not able to receive notifications when
a request fails/is done.
Code:
[networkQueue cancelAllOperations];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setUploadProgressDelegate:self.progressIndicator];
[networkQueue setDelegate:self];
[networkQueue setQueueDidFinishSelector:#selector(queueDidFinish)];
NSURL *urlAttachment = [NSURL URLWithString:#"http://localhost/test1.xml"]];
ASIFormDataRequest *requestFile = [[[ASIFormDataRequest alloc] initWithURL:urlAttachment] autorelease];
[requestFile setFile:filePath forKey:#"attachment[test]"];
[requestFile setDidFailSelector:#selector(test1WentWrong)];
[requestFile setDidFinishSelector:#selector(test1Done)];
[networkQueue addOperation:requestFile]; //queue is an NSOperationQueue
NSURL *url = [NSURL URLWithString:#"http://localhost/test2.xml"]];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:test.filename forKey:#"filename[test]" ];
[request setDidFailSelector:#selector(test2WentWrong)];
[request setDidFinishSelector:#selector(test2Done)];
[networkQueue addOperation:request]; //queue is an NSOperationQueue
[networkQueue go];
test1WentWrong, test1Done, test2WentWrong, test2Done aren't called.
Although the request runs fine and queueDidFinish gets called.
You need to set the delegate of the individual requests rather than the queue.
Basically, if you set the didFinish and didFail selectors on the queue, the queue's delegate is called. If you set them on the request, the request's delegate is called (You can also do both, in which case both get called).
In your case, where you want to use the same delegate for both requests, but different selectors for didFail / didFinish, I can see it would make sense for the queue's delegate to be called if you didn't set a delegate for the request. Perhaps I should add this... :)
Ben