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 回答