在我的应用程序中,我在NSOperationQueue中执行10个异步NSURLConnections作为NSInvocationOperations.为了防止每个操作在连接有机会完成之前返回,我调用CFRunLoopRun(),如下所示:
- (void)connectInBackground:(NSURLRequest*)URLRequest {
TTURLConnection* connection = [[TTURLConnection alloc] initWithRequest:URLRequest delegate:self];
// Prevent the thread from exiting while the asynchronous connection completes the work. Delegate methods will
// continue the run loop when the connection is finished.
CFRunLoopRun();
[connection release];
}
Run Code Online (Sandbox Code Playgroud)
连接完成后,最终连接委托选择器调用CFRunLoopStop(CFRunLoopGetCurrent())以恢复connectInBackground()中的执行,允许它正常返回:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
TTURLConnection* ttConnection = (TTURLConnection*)connection;
...
// Resume execution where CFRunLoopRun() was called.
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
TTURLConnection* ttConnection = (TTURLConnection*)connection;
...
// Resume execution where CFRunLoopRun() was called.
CFRunLoopStop(CFRunLoopGetCurrent());
} …Run Code Online (Sandbox Code Playgroud) 好吧,它可能听起来很愚蠢,但我需要找到一段代码来快速温暖iPhone并且不会冻结整个应用程序.有没有人曾经处理过热量和手机?