我有一个 iphone 应用程序,在加载视图控制器时,我想在后台线程上调用 Web 服务、获取和解析 XML。然后在线程完成后更新ui,然后触发另一个线程在另一个后台线程中执行第二次操作。
有点像链接线程调用:
- UI 线程 -> 创建 BG 线程
- BG 线程 -> 调用 XML 服务并获取结果
- UI Thread -> 更新 BG Thread 操作成功的 UI
- BG Thread -> 触发操作的第 2 部分
全部加载应用程序。
问题是我的第一个 BG 线程操作似乎永远不会结束。我waitUntilAllOperationsAreFinished
在第 2 步完成后添加了一个电话,只是为了看看发生了什么,我的应用程序似乎永远不会超过那个点。
这是一种基本的骨架实现:
- (void) viewDidLoad
{
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
//other stuff
[self loadFriendsInBackgroundThread];
}
- (void) loadFriendsInBackgroundThread
{
NSInvocationOperation *operation = [NSInvocationOperation alloc];
operation = [operation initWithTarget:self selector:@selector(invokeLoadingOfFriends:) object: nil];
[queue addOperation:operation];
[operation release];
}
- (void) invokeLoadingOfFriends: (id) obj
{
//webservice calls and results
[self performSelectorOnMainThread:@selector(invokeRefreshAfterLoadingFriends:)
withObject:nil
waitUntilDone:YES];
}
- (void) invokeRefreshAfterLoadingFriends: (id) obj
{
//this line is where is hangs
[queue waitUntilAllOperationsAreFinished];
//never gets here
[self refresh: NO];
}
关于为什么第一个线程调用似乎永远不会结束的任何想法?
感谢您可以给马克的任何帮助