iphone ios线程问题:使用dispatch_async时委托对象没有响应
这是我的代码:
MainDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FetchManager *fetchManager = [FetchManager sharedInstance];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
[fetchManager fetchData];
});
//regular code continues
}
FetchData.m(这是一个单例类)
- (id)getInstance { ... }
- (void)fetchData
{
NSURL *url = [NSURL URLWithString:@"http://www.data.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}
[xmlData release];
xmlData = [[NSMutableData alloc] init];
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
/*and all necessary NSXMLParserDelegate protocol methods are implemented - not shown here*/
问题:没有任何 NSXMLParserDelegate 协议方法被触发。我不知道为什么。如果我删除 didFinishLaunchingWithOptions 方法中的“dispatch_async”,那么事情就会按预期工作 - NSXMLParserDelegate 协议方法被触发。
有人看到问题了吗?
Here's my code:
MainDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FetchManager *fetchManager = [FetchManager sharedInstance];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
[fetchManager fetchData];
});
//regular code continues
}
FetchData.m(this is a singleton class)
- (id)getInstance { ... }
- (void)fetchData
{
NSURL *url = [NSURL URLWithString:@"http://www.data.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}
[xmlData release];
xmlData = [[NSMutableData alloc] init];
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
/*and all necessary NSXMLParserDelegate protocol methods are implemented - not shown here*/
Problem: none of the NSXMLParserDelegate protocol methods are fired. I do not know why. If I remove the "dispatch_async" in the didFinishLaunchingWithOptions method, then things are working as expected - the NSXMLParserDelegate protocol methods are fired.
Anyone see the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事显而易见:
NSURLConnection 需要一个运行循环才能运行。当您在主线程上运行时,会自动出现一个运行循环,连接将在该循环上自行调度。当您使用dispatch_async()时,您的块被调度到某个辅助线程中,该辅助线程可能没有运行循环,或者其运行循环可能不处于允许NSURLConnection运行的模式。
即使连接可以自行调度,块也会在连接创建后立即结束。 NSURLConnection 期望在创建它的线程上发送其委托消息,但是如果块已经结束,那将如何工作?是否存在某种有效的上下文,可以在该线程上调用委托?
Two things jump out:
NSURLConnection requires a run loop to operate. When you run on the main thread, there's automatically a run loop that the connection will schedule itself on. When you use dispatch_async(), your block is scheduled in some secondary thread that may not have a run loop, or whose run loop may not be in a mode that allows NSURLConnection to operate.
Even if the connection could schedule itself, the block ends right after the connection is created. NSURLConnection expects send its delegate messages on the thread where it was created, but how would that work if the block has ended? Is there some sort of valid context in which the delegate can be called on that thread?
正如 Caleb 所说,NSURLConnection 需要一个运行循环。完成您想要的任务的一种方法是创建 NSOperation 的子类,该子类使循环保持活动状态并管理委托回调。另一种方法是使用这个令人惊奇的新 Api,它只需要创建一个 NSOperationQueue,但仅在 iOS5 上可用
sendAsynchronousRequest:queue:completionHandler
As stated by Caleb NSURLConnection needs a run loop. One way to accomplish what you want is create a subclass of NSOperation that keeps the loop alive and that manages delegation callbacks. the other way around is use this amazing new Api that just need the creation of an NSOperationQueue, but is only available on iOS5
sendAsynchronousRequest:queue:completionHandler