我遇到了一个问题。场景如下:我得到了一个NSOperationQueue,其中包含需要等待UntilDone:YES的各种NSOperationQueue。当队列或操作正在运行时,我也需要更新UI。处理这种情况的最佳方法是什么?
我已经尝试过performSelectOnMainThread,但每次需要更新UI时都有必要使用这种方法吗。这似乎不是一个好的解决方案。
- (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];
}
所以每次更新UI时,我都需要调用
[_collectionCoverView performSelectorOnMainThread:@selector(setDownloadProgress:)
withObject:[NSNumber numberWithFloat:progress]
waitUntilDone:YES];
Is there any appropriate way to handle the UI?
我对您的代码没有太多理解。但是,为了实现您想要的,您可以在AFOperation方法的末尾添加UI更新代码。我的意思是,如果你在操作方法中添加它,一旦完成一些处理,UI就会自动更新。
UI更新通常也发生在MainThread中。因此,调用performSelectionInMainThread没有错。