Magic of NSOperation internals - how does it observe isFinished key so that completionBlock is always run?

你说的曾经没有我的故事 提交于 2019-12-06 02:14:49

This is what I currently know about NSOperation's lifecycle, it is not much but is pretty enough to answer my original question:

  1. NSOperation registers KVO observation of its isFinished property in its -init method. It means that the observation is registered even if I don't ever run -start (main) method (I did some experiments with custom NSOperation subclass to determine that). Corresponding un-registration is done in NSOperation's -dealloc method (I can't prove that, but it is really the only place where it can happen).

  2. To make KVO possible and "private" NSOperation has some internal container class NSOperationInternal which encapsulates NSOperation's own KVO routines giving me a freedom to do my custom KVO when I want implement it for the instances of my custom subclasses of NSOperation class. In my custom NSOperation subclasses I can use my own implementation of observeValueForKeyPath:ofObject:change:context: method without worrying about any possible conflicts with NSOperation's own KVO because it would not interfere with this method implemented in NSOperationInternal.

P.S. Regarding the word 'magic' - I know it is all KVO behind the curtains - so no surprises here about KVO itself. My surprises are how KVO is applied to NSOperation in concrete details: nothing magical is here, but it was not really obvious, when I first began introducing my own KVO and became suspicious about possible interference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!