ASIHTTP是否支持多线程? [英] Does ASIHTTP support multi threads?
问题描述
ASIHTTP是否支持多线程?如果可以的话,我希望每个线程都链接到aUIProgressbar,该如何构造代码?
Does ASIHTTP support multi thread? If sure, I hope each thread link to aUIProgressbar, how can I construct the codes?
谢谢
interdev
推荐答案
是的,ASIHttpRequest一次可以处理更多内容.请参阅帮助文档,并注意这一部分.
Yups, ASIHttpRequest can handle more than on request at a time. See the help documentation and do notice this part.
使用队列
此示例执行相同的操作再一次,但是我们已经创建了一个NSOperationQueue用于我们的请求.
This example does the same thing again, but we've created an NSOperationQueue for our request.
使用NSOperationQueue(或ASINetworkQueue,请参见下文)为您提供对异步的更多控制要求.使用队列时,只有一个一定数量的请求可以在同时.如果您添加更多请求比队列的请求多maxConcurrentOperationCount属性,请求将等待其他人在他们开始之前完成.
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)发送者{如果(![自队列]){[self setQueue:[[[[NSOperationQueue alloc]init] autorelease]];}NSURL * url = [NSURL URLWithString:@" http://allseeing-i.com "];ASIHTTPRequest *请求=[ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[要求setDidFinishSelector:@selector(requestDone :)];[要求setDidFailSelector:@selector(requestWentWrong :)];[[自队列] addOperation:request];//队列是一个NSOperationQueue}
(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)requestDone:(ASIHTTPRequest *)request { NSString *response = [request responseString]; }
(void)requestWentWrong:((ASIHTTPRequest*)request {NSError * error = [request error];}
(void)requestWentWrong:(ASIHTTPRequest *)request { NSError *error = [request error]; }
在上面的示例中,队列"是一个保留的NSOperationQueue属性我们的控制器.
In the above sample, ‘queue’ is a retained NSOperationQueue property of our controller.
我们正在设置自定义选择器请求时将被调用成功或失败.如果您不设定这些是默认值(requestFinished:和requestFailed :)将被使用,因为在上一个示例中.
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:
- 如果您的请求都是相同的广泛类型,但您想区分它们,您可以设置的userInfo NSDictionary属性每个请求都带有您自己的自定义数据您可以读完/失败的委托方法.
- 如果您需要以完全不同的方式处理成功和失败每个请求的方式,设置不同的setDidFinishSelector/每个请求的setDidFailSelector
- 对于更复杂的情况,或者您想解析响应的地方在后台,创建一个最小的每个的ASIHTTPRequest子类请求类型和覆盖requestFinished:和failWithProblem:.
希望这会有所帮助.
谢谢
Madhup
这篇关于ASIHTTP是否支持多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!