iOS:ASIHTTPREQUST在背景选择器上同步不好的主意?
在后台选择器上运行之前,我以同步方式使用 NSURLConnection,因此当我转向 ASIHTTPRequest 时,我对此框架做了同样的事情。
那么,执行以下操作是一个坏主意吗?
// 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 和异步方法与委托方法一起使用有什么好处?
I was using NSURLConnection in a synchronous way before running on a background selector, so when I moved over to ASIHTTPRequest I did the same with this framework.
So, is it a bad idea to do something like the following?
// 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
What would be the advantage to use the ASIHTTPRequest and asynchronous methods along with the delegate methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据经验,在辅助线程中使用
ASIHTTPRequest
同步请求时,有时会发生奇怪的事情:状态栏中的下载活动图标在下载完成后不会消失,这是我不时注意到的一个问题。我过去没有遇到什么大问题,但我现在使用异步方法而不是你的方法。 ASI 异步方法本质上是一个广泛使用的库,比我自己的实现经过了更严格的测试。使用异步方法有很多优点 - 你提到了委托方法,但最新版本的 ASI 实际上也支持块,这是一个巨大的飞跃(处理多个同步调用过去有点痛苦,因为共享委托方法(或每个异步调用的唯一委托),但现在您可以完全摆脱委托,而且如果您使用多个贡献者,它可以提高可读性。更轻松。
From experience, sometimes odd things can happen when using
ASIHTTPRequest
synchronous requests off a secondary thread: the download activity icon in the status bar not disappearing upon download completion is one issue I've noticed from time to time. I've had no major problems in the past, but I use the asynchronous methods now rather than your approach. The ASI asynchronous methods are by the nature of being a widely used library more highly tested than my own implementation could ever be.There are a number of advantages with using the asynchronous methods - you mention the delegate methods, but the latest release of ASI actually also supports blocks, which is a great leap forward (dealing with multiple synchronous calls used to be a bit of a pain due to the shared delegate methods (or unique delegates for each asynchronous call). But with blocks you can now get rid of the delegates entirely. I've found them to be really useful. Plus if you use multiple contributors it can make readability a lot easier.
此外,通过异步执行,您可以通过 setProgressDelegate 命令更轻松地跟踪进度。
Also, by doing it Async, you can more easily track progress through the setProgressDelegate command.