2012-03-17 7 views
0

問題が発生しました。このシナリオは次のようなものです:NSOperationQueueには、waitUntilDone:YESを必要とするさまざまなNSOperationQueueが含まれています。また、キューや操作が実行されているときにUIを更新する必要があります。この状況を処理する最善の方法は何ですか?NSOperationQueue waitUntilFinishedのため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? 
+0

UIを更新するまでキューを待つことを意味しますか? – Vignesh

+0

キューとUIを同時に実行できますか? – Lunayo

+0

Ya。間違いなく可能です。有りうる。 – Vignesh

答えて

0

を呼び出す必要があります。しかし、あなたが望むものを達成するためには、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

関連する問題