1

オブザーバーがこれ以上操作がないことを通知すると、関数は呼び出されません (performSelector...)。面白いことに、NSLog(@"queue has completed") が正しくログに記録されます。

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                     change:(NSDictionary *)change context:(void *)context
{
if (object == self.operationQueue && [keyPath isEqualToString:@"operations"]) {
    if ([self.operationQueue.operations count] == 0)
    {
        [self performSelector:@selector(refreshCollectionView) withObject:nil afterDelay:0.2];
        // Do something here when your queue has completed
        NSLog(@"queue has completed");

    }
}
else {
    [super observeValueForKeyPath:keyPath ofObject:object
                           change:change context:context];
}
}

編集

とった:

dispatch_async(dispatch_get_main_queue(), ^{
             [self performSelector:@selector(refreshCollectionView) withObject:nil afterDelay:0.2];
             });

なぜ performSelectorOnMainThread... が機能しなかったのかわかりませんが、このように機能します。

4

1 に答える 1

1

オブザーバーがキューと同じスレッドで起動されている場合、完了時にキューのスレッドがリープされている可能性が非常に高くなります。-performSelector:...afterDelay: は実行中の実行ループを必要とするため、床に落とされる可能性があります。

とにかくUIを更新しているので、メインスレッドでそのセレクターを実行してください。

于 2013-05-13T21:54:24.953 に答える