0

問題が発生しました。シナリオは次のようになります。waitUntilDone:YESが必要なさまざまなNSOperationQueueを含むNSOperationQueueを取得しました。また、キューまたは操作の実行中に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?
4

1 に答える 1

0

私はあなたのコードから多くを理解していませんでした。ただし、目的を達成するために、AFOperationメソッドの最後にUI更新コードを追加できます。つまり、操作メソッドにUIを追加すると、何らかの処理が完了するとUIが自動的に更新されます。

また、通常、UIの更新はMainThreadで行われます。したがって、performSelectorInMainThreadの呼び出しに問題はありません。

于 2012-03-17T06:40:36.517 に答える