Iphone iOS:AsiHttpRequest后台同步选择器坏主意?
在后台选择器上运行之前,我以同步方式使用了NSURLConnection,所以当我转到ASIHTTPRequest时,我也使用了这个框架 那么,这样做是不是一个坏主意Iphone iOS:AsiHttpRequest后台同步选择器坏主意?,iphone,objective-c,ios,asihttprequest,Iphone,Objective C,Ios,Asihttprequest,在后台选择器上运行之前,我以同步方式使用了NSURLConnection,所以当我转到ASIHTTPRequest时,我也使用了这个框架 那么,这样做是不是一个坏主意 // From another method [self performSelectorInBackground:@selector(callDatasource) withObject:nil]; - (NSData *)callDatasource { NSAutoreleasePool *apool = [[N
// From another method
[self performSelectorInBackground:@selector(callDatasource) withObject:nil];
- (NSData *)callDatasource {
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:someURLthatIamusing];
[request setTimeOutSeconds:50.0];
[request startSynchronous];
NSError *error = [request error];
NSData *returnedData;
if (!error) {
returnedData = [request responseData];
} else {
// do something with error
}
[self performSelectorOnMainThread:@selector(done) withObject:nil waitUntilDone:NO];
[apool release];
return returnedData;
}//end
将ASIHTTPRequest和异步方法与委托方法一起使用有什么好处?根据经验,有时,在使用辅助线程下的
ASIHTTPRequest
同步请求时,会发生一些奇怪的事情:下载完成后,状态栏中的下载活动图标不会消失是我不时注意到的一个问题。我过去没有遇到过大的问题,但我现在使用异步方法,而不是您的方法。ASI异步方法本质上是一个被广泛使用的库,比我自己的实现要经受更高的测试
使用异步方法有许多优点-您提到了委托方法,但ASI的最新版本实际上也支持块,这是一个巨大的飞跃(由于共享委托方法(或每个异步调用的唯一委托),处理多个同步调用过去有点痛苦)。但是使用块,您现在可以完全摆脱委托。我发现它们非常有用。此外,如果您使用多个贡献者,则可读性会大大降低。此外,通过异步执行,您可以更轻松地通过setProgressDelegate命令跟踪进度