2013-05-13 85 views
1

當我的觀察員告訴我沒有更多的操作時,函數不會被調用(performSelector ...)。有趣的是,NSLog(@「queue has completed」)被正確記錄。NSOperationQueue完成不會啓動新方法

- (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 ...沒有工作,但它以這種方式工作。

回答

1

如果您的觀察者正在與隊列相同的線程中被觸發,那麼隊列的線程很可能會在完成時被收回。由於-performSelector:... afterDelay:需要一個正在運行的運行循環,因此可能會丟在地板上。

由於您正在更新UI,請在主線程上執行該選擇器。

+0

任何建議如何? – user1832330 2013-05-13 22:16:31

+0

得到它,但無論如何不工作:/任何方法不會開始(即使我把refreshCollectionView的東西放入隊列完整語句內) – user1832330 2013-05-13 22:22:53