我正在尝试了解iOS中的多线程编程。
这段代码可正常工作且回调符合预期。
在文档中https://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html#//apple_ref/occ/instm/NSRunLoop/run中提到,'run'方法'将接收者置于永久循环中,在此期间它会处理所有附加的输入源的数据。'
现在,在上述代码中,我没有将任何源附加到runLoop上。它是如何工作的?
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?
});
这段代码可正常工作且回调符合预期。
在文档中https://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html#//apple_ref/occ/instm/NSRunLoop/run中提到,'run'方法'将接收者置于永久循环中,在此期间它会处理所有附加的输入源的数据。'
现在,在上述代码中,我没有将任何源附加到runLoop上。它是如何工作的?