【发布时间】:2014-07-16 11:13:21
【问题描述】:
我试图了解 iOS 中的多线程编程。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
, ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
NSLog(@"Request failed");
} else {
NSLog(@"Request sent");
}
[[NSRunLoop currentRunLoop] run];//How does this work?
});
这段代码运行良好,我得到了预期的回调。
提到'run'方法,'将接收器置于一个永久循环中,在此期间它处理来自所有附加输入源的数据。'
现在,在上面的代码中,我没有将任何源附加到 runLoop。它是如何工作的?
【问题讨论】:
-
为什么需要执行[[NSRunLoop currentRunLoop] run]?
-
获取 NSURLConnectionDelegate 回调。没有这个,回调将不会被调用。
标签: ios objective-c multithreading grand-central-dispatch nsrunloop