Objective c 用户界面没有';无法更新,因为NSOperationQueue waitUntilFinished是
我有个问题。场景是这样的:我得到了一个NSOperationQueue,其中包含需要等待的各种NSOperationQueue。我还需要在队列或操作运行时更新UI。处理这种情况的最佳方法是什么 我已经尝试过performSelectorOnMainThread,但是每次需要更新UI时都需要使用此方法吗。这似乎不是一个好的解决办法Objective c 用户界面没有';无法更新,因为NSOperationQueue waitUntilFinished是,objective-c,ios,nsoperationqueue,nsinvocationoperation,Objective C,Ios,Nsoperationqueue,Nsinvocationoperation,我有个问题。场景是这样的:我得到了一个NSOperationQueue,其中包含需要等待的各种NSOperationQueue。我还需要在队列或操作运行时更新UI。处理这种情况的最佳方法是什么 我已经尝试过performSelectorOnMainThread,但是每次需要更新UI时都需要使用此方法吗。这似乎不是一个好的解决办法 - (void)loadPreviewPageWithMagazineID:(NSString *)magazineID userID:(NSString *)user
- (void)loadPreviewPageWithMagazineID:(NSString *)magazineID userID:(NSString *)userID {
NSMutableArray *operationArray = [NSMutableArray array];
for (NSInteger i = 1; i <= _numTotalPages; ++i) {
//NSLog(@"currenpage = %d, index = %d",_selectedPage,pageIndex);
NSDictionary *arguments = [NSDictionary dictionaryWithObjectsAndKeys:magazineID,
@"itemID", userID, @"userID", [NSNumber numberWithInt:i],
@"pageNumber", nil];
AFOperation *imageOperation =
[[AFOperation alloc] initWithTarget:self
selector:@selector(savePageToDisk:)
object:arguments];
[imageOperation addObserver:self forKeyPath:@"isFinished" options:0 context:nil];
[imageOperation setUserInfo:arguments];
[operationArray addObject:imageOperation];
[imageOperation release];
}
[_imageQueue addOperations:operationArray waitUntilFinished:YES];
}
- (void)processingMagazine:(NSDictionary *)arguments {
// load pdf document from decrypted data
NSString *userID = [arguments objectForKey:@"userID"];
NSString *magazineID = [arguments objectForKey:@"itemID"];
[self loadPreviewPageWithMagazineID:magazineID userID:userID];
}
我对你的代码理解不多。但是为了实现您想要的,您可以在AFOperation方法的末尾添加UI更新代码。我的意思是,如果您将UI添加到操作方法中,UI将在完成某些处理后自动更新
通常UI更新也发生在主线程中。因此,调用performSelectorInMainThread没有错。您的意思是说您的队列等待更新UI?队列和UI是否可以同时运行?是的。这绝对是可能的。这是可能发生的。苹果开发者参考说,“操作总是在一个单独的线程上执行,不管它们被指定为并发操作还是非并发操作”。但是我将NSOperationQueue设置为
waitUntilFinished:YES
,这样UI就不会更新。我应该发布代码吗?好的,我想要的是这样的:一个NSOperationQueue a,一个NSOperationQueue B。这个B队列需要处理一个需要等待完成的进程。然后是一个addOperation,一个操作B队列的操作,当B队列完成后,我更新UISo,基本上就像一个队列操作另一个队列一样(waitUntilFinished:YES)。奇怪的是,当B队列运行时,主线程挂起,尽管我已经将它放在另一个队列中。我想我应该调用performSelectorInMainThread
。谢谢
[_collectionCoverView performSelectorOnMainThread:@selector(setDownloadProgress:)
withObject:[NSNumber numberWithFloat:progress]
waitUntilDone:YES];
Is there any appropriate way to handle the UI?