2012-03-17 4 views
0

문제가 있습니다. 시나리오는 다음과 같습니다. waitUntilDone : YES가 필요한 다양한 NSOperationQueue를 포함하는 NSOperationQueue를 얻었습니다. 대기열 또는 작업이 실행 중일 때 UI를 업데이트해야합니다. 이 상황을 처리하는 가장 좋은 방법은 무엇입니까?NSOperationQueue waitUntilFinished 때문에 UI가 업데이트되지 않았습니다.

performSelectorOnMainThread를 시도했지만 UI를 업데이트해야 할 때마다이 메서드를 사용해야합니다. 좋은 해결책이 아닌 것 같습니다. 난 당신의 코드에서 많은 것을 이해 didn를

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

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

UI를 업데이트 할 때까지 기다리는 대기열을 말하고 싶습니까? – Vignesh

+0

대기열과 UI가 동시에 실행될 수 있습니까? – Lunayo

+0

예. 확실히 가능합니다. 그것은 일어날 수있다. – Vignesh

답변

0

- (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를 업데이트합니다. 그러나 원하는 것을 이루기 위해 AFOperation 메서드가 끝날 때 UI 업데이트 코드를 추가 할 수 있습니다. 작동 방법에 추가하면 일부 처리가 완료되면 UI가 자동으로 업데이트됩니다.

또한 일반적으로 UI 업데이트는 MainThread에서 발생합니다. performSelectorInMainThread를 호출해도 아무런 문제가 없습니다.

+0

을 게시했다. 원하는 것은 다음과 같다 : NSOperationQueue A, NSOperationQueue B.이 B 큐는 완료를 기다릴 필요가있는 프로세스를 처리해야한다. 그리고 나서 addOperation은 B 큐를 조작하는 연산이고, b 큐가 끝나면 UI를 업데이트합니다. – Lunayo

+0

그래서 기본적으로 다른 큐 (waitUntilFinished : YES)를 작동시키는 큐와 같습니다. B 큐가 실행 중일 때 다른 큐에 놓았지만 이상한 것은 주 스레드입니다. – Lunayo

+0

나는'performSelectorInMainThread'를 호출해야한다고 생각합니다. 감사! – Lunayo

관련 문제