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?
Lunayo
  • 538
  • 7
  • 32

1 Answers1

0

I didn understand much from your code. But to accomplish what you want, you can add the UI update code at the end of your AFOperation method. I mean to say, UI will be updated automatically once some processing is done, if you add it in the operation method.

Also generally UI update happens in MainThread. SO there is nothing wrong in calling performSelectorInMainThread.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • ok what I want is like this: a NSOperationQueue A, NSOperationQueue B. and this B queue need to handle a process that need to be waiting for finished. and then A addOperation an operation that operate B queue, and when the b queue has finished, I update the UI – Lunayo Mar 17 '12 at 06:42
  • So basically it is like a queue that operate another queue(waitUntilFinished:YES). The weird things is the main thread is Hang when the B queue is running, although I have place it in another queue. – Lunayo Mar 17 '12 at 06:49
  • I think I should call the `performSelectorInMainThread`. Thanks! – Lunayo Mar 17 '12 at 06:56