facebookarchive / KVOController

Simple, modern, thread-safe key-value observing for iOS and OS X.
Other
7.34k stars 925 forks source link

what should i do if I'd like to use like this without retain cycle #119

Closed xuzhenhao closed 8 years ago

xuzhenhao commented 8 years ago

[self.KVOController observe:self keyPath:@"pages" options:NSKeyValueObservingOptionNew action:@selector(pagesDidChange:object:)];

nlutsenko commented 8 years ago

There is KVOControllerNotRetaining just for that... :) You would need to add [self.KVOControllerNotRetaining unobserveAll] in your dealloc just to make sure nothing gets still observed when you are deallocating the object.

I would suggest refactoring your class, to avoid observation on self, since you have full control over it and can override the property getter/setter to get the same behavior.

Let me know if this helps.

DreamBuddy commented 7 years ago

@nlutsenko When I added the [self.KVOControllerNotRetaining unobserveAll] in my dealloc . it crashed.. " was deallocated while key value observers were still registered with it "

ozgur commented 6 years ago

It is an old thread but for @nlutsenko's and others information, the problem still persists in versions prior to iOS 11, even when you use self.KVOControllerNotRetaining and call [self.KVOControllerNotRetaining unobservall] in dealloc. I think if you need to observe self, it is not a good idea to do that with KVO or any 3rd party like this one. You just override the setter method and you'll be good to go.