0

I got an issue. The scenario is like this: I got an NSOperationQueue that contain various NSOperationQueue that need to waitUntilDone:YES. And I need to update the UI too as the queue or the operation is running. What is the best way to handle this situation?

I have tried performSelectorOnMainThread, but is it necessary to use this method every time I need to update the UI. It is seems not a good solution.

- (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];
}

So each time to update UI I need to call

[_collectionCoverView performSelectorOnMainThread:@selector(setDownloadProgress:)
                                       withObject:[NSNumber numberWithFloat:progress] 
                                    waitUntilDone:YES];

Is there any appropriate way to handle the UI?
4

1 回答 1

0

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

UI 更新通常也发生在 MainThread 中。所以调用 performSelectorInMainThread 没有错。

于 2012-03-17T06:40:36.517 回答