2012-03-17 38 views
0

我有问题。这种情况是这样的:我得到了一个N​​SOperationQueue,其中包含需要等待的所有NSOperationQueue:YES。我还需要在队列或操作正在运行时更新UI。处理这种情况的最佳方法是什么?UI没有更新,因为NSOperationQueue waitUntilFinished YES

我已经尝试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]; 
} 

所以

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

Is there any appropriate way to handle the UI? 
+0

你的意思是说你的队列等待直到你更新你的UI? – Vignesh 2012-03-17 05:53:36

+0

队列和UI可以同时运行吗? – Lunayo 2012-03-17 06:07:32

+0

雅。这绝对有可能。这有可能发生。 – Vignesh 2012-03-17 06:08:43

回答

0

我不知道多少从您的代码每次更新UI我需要调用。但要完成你想要的,你可以在你的AFOperation方法的末尾添加UI更新代码。我的意思是说,一旦某些处理完成后,UI将自动更新,如果您将其添加到操作方法中。

一般UI更新发生在MainThread中。所以调用performSelectorInMainThread没有任何问题。

+0

好的我想要的是这样的:NSOperationQueue A,NSOperationQueue B.这个B队列需要处理需要等待完成的进程。然后A addOperation操作B队列的操作,并且当B队列完成时,我更新UI – Lunayo 2012-03-17 06:42:44

+0

所以基本上它就像是一个运行另一个队列的队列(waitUntilFinished:YES)。奇怪的是,当B队列正在运行时,主线程为Hang,尽管我已将它放在另一个队列中。 – Lunayo 2012-03-17 06:49:53

+0

我想我应该调用'performSelectorInMainThread'。谢谢! – Lunayo 2012-03-17 06:56:52