NSURLConnection 和中央调度
是否建议将 NSUrlConnection 包装在 gcd 样式块中并在 low_priority 队列上运行?
我需要确保我的连接不会发生在主线程上,并且连接需要异步。我还需要同时发出多个请求。
如果我走 gcd 路线,我不确定在哪个线程上调用 NSUrlConnectionDelegate 方法。
NSURLConnection 依赖于委托,因此一旦连接完成,无论处理它的包装类都需要调用其调用者。我不确定如何处理连接工作启动/完成时调用的所有各种回调:
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incrementalData;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
我应该只调用同步版本但包装在 gcd 块中吗?如果我想取消呼叫,请使用“dispatch_suspend”?
dispatch_async(queue,^{
NSString* result = [self mySynchronousHttp:someURLToInvoke];
});
// If I need to cancel
dispatch_suspend(queue);
Is it advisable to wrap up NSUrlConnection in a gcd style blocks and run it on a low_priority queue?
I need to ensure that my connections are not happening on the main thread and the connections need to be asynchronous. I also need several simultaneous requests to go at once.
If I go the gcd route, I'm not sure which thread the NSUrlConnectionDelegate methods get invoked on.
NSURLConnection relies on delegates so once the connection is complete, whatever wrapper class that handles it will need to invoke its caller. I'm not certain how to deal with all of the various callbacks that are invoked when the connection work starts up/finishes:
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incrementalData;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Should I just call the synchronous versions but wrapped in gcd blocks? And if I want to cancel a call, use 'dispatch_suspend'?
dispatch_async(queue,^{
NSString* result = [self mySynchronousHttp:someURLToInvoke];
});
// If I need to cancel
dispatch_suspend(queue);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我推荐你看WWDC Sessions关于iPhone OS中的网络应用。
讲师说
用于网络编程,建议使用 RunLoop 进行异步网络编程。使用后台线程(Grand Central Dispatch Concurrent Queue)进行线程安全的数据处理,而不是用于网络编程。
顺便说一句,Blocks 和 Grand Central Dispatch 会议也值得一看。
我为异步 NSURLConnection 编写了一个包装类。
AsyncURLConnection.h
AsyncURLConnection.m
如何使用 AsyncURLConnection 类。
I recommend you to see WWDC Sessions about network application in iPhone OS.
The lecturer said
for network programming and recommended to use asynchronous network programming with RunLoop. Use background thread (Grand Central Dispatch Concurrent Queue) for thread-safe data processing, not for network programming.
By the way, Blocks and Grand Central Dispatch sessions are also worth to see.
I wrote a wrapper class for asynchronous NSURLConnection.
AsyncURLConnection.h
AsyncURLConnection.m
How to use AsyncURLConnection class.
创建一个并发 NSOperation,在其上运行异步 NSURLConnection。
Create a concurrent NSOperation on which you run your asynchronous NSURLConnection.
看一下这个代码块:
Have a look at this code block: