我有一个iPhone应用程序,其中一个视图控制器的负荷,我想调用Web服务,获取并解析XML所有在后台线程。然后在线程完成后更新ui,然后发起另一个线程在另一个后台线程中执行secondard操作。NSOperationQueue队列waitUntilAllOperationsAreFinished挂
有点像链线程调用:
- UI主题 - >创建BG螺纹
- BG主题 - >通话XML服务和得到的结果
- UI线程 - >成功的更新UI该BG线程操作
- BG螺纹 - >关火上的应用程序的负荷操作
所有的第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];
}
任何想法,为什么第一个线程调用似乎永远不会结束?
感谢您的帮助,您可以给 马克
哦,谢谢,不敢相信我错过了一个,由于某种原因,我假定waitUntilDone参数做了一些不同的:)谢谢 – Mark 2010-07-29 11:05:56
嗨亚历克斯,我得到了一个问题。该方案是这样的:我有一个NSOperationQueue包含需要各种NSInvocationOperation到'[_queue addOperation:操作waitUntilFinished:YES]'。我还需要在队列或操作正在运行时更新UI。处理这种情况的最佳方法是什么? – Lunayo 2012-03-17 06:28:49