We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NSURLConnection afn2.0中开启了子线程的runloop, NSURLSession afn3.0中怎么理解
The text was updated successfully, but these errors were encountered:
简单说下我的理解吧,仅供参考。
首先,如果我们要用 NSURLConnection 来实现异步请求,该怎么做到?有两种方式:
sendAsynchronousRequest:queue:completionHandler:
initWithRequest:delegate:startImmediately:
另外附一段 YY 大神对 NSURLConnection 的解析:通常使用 NSURLConnection 时,你会传入一个 Delegate,当调用了 [connection start] 后,这个 Delegate 就会不停收到事件回调。实际上,start 这个函数的内部会会获取 CurrentRunLoop,然后在其中的 DefaultMode 添加了4个 Source0 (即需要手动触发的Source)。CFMultiplexerSource 是负责各种 Delegate 回调的,CFHTTPCookieStorage 是处理各种 Cookie 的。
[connection start]
AFNetworking 2.0 和 SDWebImage 3.x 中就是用的上面的第二种方式,在支持异步的 NSOperation 子类中包装一个 NSURLConnection+runloop,然后由 NSOperationQueue 来管理这些 NSOperation 对象。
AFNetworking 3.0 中使用的是 NSURLSession,NSURLSession 是 iOS7 中新增的接口,表面上是和 NSURLConnection 并列的,但底层仍然用到了 NSURLConnection 的部分功能 (比如 com.apple.NSURLConnectionLoader 线程),实际上 NSURLSession 就是扮演着原来 AFNetworking 2.x 的角色,帮我们在异步请求时管理子线程和 runloop。
其实,NSURLConnection 和 NSURLSession 的关键就在于 runloop。
你可以自己写一个简单的 demo,分别在 NSURLConnection 和 NSURLSession 的代理方法回调中创建断点,在 LLDB 中执行 po [NSRunloop currentLoop] 和 po [NSRunloop mainLoop] 看下内容,可以窥见一些端倪。
po [NSRunloop currentLoop]
po [NSRunloop mainLoop]
Sorry, something went wrong.
No branches or pull requests
NSURLConnection afn2.0中开启了子线程的runloop, NSURLSession afn3.0中怎么理解
The text was updated successfully, but these errors were encountered: