In Swift 4, how do I remove a block-based KVO observer?
Solution 1:
In iOS 11, you don't have to. Just let the observer go out of scope. There is no penalty any longer for letting an observer die before the observed or for letting the observed die before the observer, so you have no actual work to do.
On the other hand, if you really want to unregister the observer, remove it from whatever is retaining it, or tell it to invalidate
. (Something must be retaining it, because if you don't persist the observer, it will die and your observer function will never be called.)
(You say "if I store an observer like this", but the way you are storing it, with let
, is a somewhat silly way to store the observer. It would be better to put it in a Set from which you can remove it later, or at least store it in a Optional var
that you can later set to nil
.)
Solution 2:
With Swift 5, I began using .observe(\.propertyName, ...)
on core data objects as the tokens automatically unregister at deinit
or an invalidate()
call on the token.
This works remarkably well until I recently noticed that I was leaking objects. I was seeing leaked NSKeyValueObservance
, NSKeyValueObservationInfo
, and NSArray
objects. After verifying that I was managing the tokens properly, I finally tracked down the problem.
If you perform an .observe()
on a Core Data object, you must keep the object as well as the token. If the object turns into a fault before you invalidate/release the token, you will leak memory. You do not crash but once it turns into a fault you will leak memory even if you free the token.