由于NSOperationQueue waitUntilFinished YES,UI未更新

时间:2012-03-17 05:50:53

标签: objective-c ios nsoperationqueue nsinvocationoperation

我遇到了一个问题。场景是这样的:我有一个NSOperationQueue包含需要waitUntilDone的各种NSOperationQueue:YES。我还需要在队列或操作运行时更新UI。处理这种情况的最佳方法是什么?

我尝试过performSelectorOnMainThread,但每次需要更新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?

1 个答案:

答案 0 :(得分:0)

我对你的代码了解不多。但要完成您想要的任务,您可以在AFOperation方法的末尾添加UI更新代码。我的意思是,如果在操作方法中添加UI,则一旦完成某些处理,UI将自动更新。

通常,在MainThread中也会进行UI更新。所以调用performSelectorInMainThread没有错。