2015-08-31 15 views
0

在SDWebImageView庫中有一個名爲「SDWebImageDownloaderOperation」的類,它具有以下方法。爲什麼在SDWebImage中使用「willChangeValueForKey:」?

- (void)setFinished:(BOOL)finished { 
    [self willChangeValueForKey:@"isFinished"]; 
    _finished = finished; 
    [self didChangeValueForKey:@"isFinished"]; 
} 

然而,RS不覆蓋automaticallyNotifiesObserversForKey:並沒有實現observeValueForKeyPath:ofObject:change:context:方法,那麼什麼是寫在這裏

[self willChangeValueForKey:@"isFinished"] 

[self didChangeValueForKey:@"isFinished"] 

回答

1

的關鍵原因是目的SDWebImageDownloaderOperationNSOperation的一個子類,並且該類不能像所有其他類一樣使用正常的KVO通知。 此操作在後臺執行某些任務,因此您需要在操作完成和何時仍在運行時通知操作系統。

Apple建議here明確呼叫willChangeValueForKeydidChangeValueForKey

如果你正在尋找一個更好的解釋爲什麼NSOPerations沒有正常的志願,你可以閱讀這個答案Why does NSOperation disable automatic key-value observing?

相關問題