【问题标题】:NSOperationQueue finished wont start new methodNSOperationQueue 完成不会启动新方法
【发布时间】:2013-05-08 01:01:30
【问题描述】:

当我的观察者告诉我没有更多操作时,不会调用函数(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... 没有工作,但它是这样工作的。

【问题讨论】:

    标签: objective-c methods selector nsoperation nsoperationqueue


    【解决方案1】:

    如果您的观察者在与队列相同的线程上被触发,则很可能队列的线程在完成时正在被回收。由于 -performSelector:...afterDelay: 需要一个正在运行的运行循环,它很可能会掉在地上。

    由于无论如何您都在更新 UI,因此请在主线程上执行该选择器。

    【讨论】:

    • 得到了它但无论如何都不起作用:/任何方法都不会启动(即使我将 refreshCollectionView 中的内容放入队列完成语句中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多