Hello everyone
Does ASIHTTP support multi thread? If sure, I hope each thread link to aUIProgressbar, how can I construct the codes?
Thanks
interdev
Hello everyone
Does ASIHTTP support multi thread? If sure, I hope each thread link to aUIProgressbar, how can I construct the codes?
Thanks
interdev
Check out the sample code here: http://allseeing-i.com/ASIHTTPRequest/How-to-use
It does support asynchronous requests (multithreaded) and you can use an ASINetworkQueue to monitor their progress.
Yups, ASIHttpRequest can handle more than on request at a time. See the help documentation and do notice this part.
Using a queue
This example does the same thing again, but we've created an NSOperationQueue for our request.
Using an NSOperationQueue (or ASINetworkQueue, see below) gives you more control over asynchronous requests. When using a queue, only a certain number of requests can run at the same time. If you add more requests than the queue's maxConcurrentOperationCount property, requests will wait for others to finish before they start.
(IBAction)grabURLInTheBackground:(id)sender { if (![self queue]) { [self setQueue:[[[NSOperationQueue alloc] init] autorelease]]; } NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)]; [request setDidFailSelector:@selector(requestWentWrong:)]; [[self queue] addOperation:request]; //queue is an NSOperationQueue }(void)requestDone:(ASIHTTPRequest *)request { NSString *response = [request responseString]; }
(void)requestWentWrong:(ASIHTTPRequest *)request { NSError *error = [request error]; }
In the above sample, ‘queue’ is a retained NSOperationQueue property of our controller.
We’re setting custom selectors that will be called when the request succeeds or fails. If you don’t set these, the defaults (requestFinished: and requestFailed:) will be used, as in the previous example.
Handling success and failure for multiple requests
If you need to handle success and failure on many different types of request, you have several options:
- If your requests are all of the same broad type, but you want to distinguish between them, you can set the userInfo NSDictionary property of each request with your own custom data that you can read in your finished / failed delegate methods.
- If you need to handle success and failure in a completely different way for each request, set a different setDidFinishSelector / setDidFailSelector for each request
- For more complex situations, or where you want to parse the response in the background, create a minimal subclass of ASIHTTPRequest for each type of request, and override requestFinished: and failWithProblem:.
Hope this helps.
Thanks,
Madhup