Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential KVO bindings issue #21

Closed
nayzak opened this issue Mar 6, 2015 · 5 comments
Closed

Potential KVO bindings issue #21

nayzak opened this issue Mar 6, 2015 · 5 comments
Labels

Comments

@nayzak
Copy link

nayzak commented Mar 6, 2015

I'm not sure and didn't test yet, but it seems to be an issue, that removeObserver is called only from deinit of KVO Helper. It may be a situation, when observable NSObject can be deallocated before Dynamic object who observers an KVO object. In this case an app will crash on iOS 8.
So I suggest to give an option for user so that he could call removeObserver when it is really necessary.

@srdanrasic
Copy link
Contributor

Thanks for the feedback Ian. Yeah, this'll need some improvement.

In addition to your suggestion, I'm thinking to allow option to retain observed NSObject by the Dynamic. Do you think that that might help in some situations?

@nayzak
Copy link
Author

nayzak commented Mar 7, 2015

I think it would be more dangerous to retain observed NSObject by the Dynamic than to ask user to stop observing directly.
Now I'm working on tiny "mvvm" framework for my own use and I found many interesting ideas in SwiftBond project (but I think it little bit complex for my taste). So I tried to make an infinite scroll for UITableView and found that issue with removeObserver. Infinite scroll object had a Dynamic instance witch observed table's contentOffset. And when table view tried to deallocate itself an app was crashed because infinite scroll object was an instance value of UITableView (and also it's subview). So I made a special Dynamic class only for KVO with special method for removing itself from NSObject observers. And than I used it in willMoveToSuperview of infinite scroll object.
I'm not a person who knows many programming patterns and is able to make a good architecture. More of that I have only starter level of knowledge in iOS development. So I don't think that I could give you any advice at this point.

@srdanrasic
Copy link
Contributor

Ok, I can see the problem. Please have in mind that Cocoa Touch does not officially support KVO on UIKit objects. It's just a coincidence it works. In your case, better approach would be to register your view controller as a delegate of your table view and implement method scrollViewDidScroll:.

UITableView is a subclass of UIScrollView and UITableViewDelegate inherits UIScrollViewDelegate so you can get offset change that way.

@nayzak
Copy link
Author

nayzak commented Mar 17, 2015

Hm. Just made infinite scroll using Bond and KVO observing. It seems to be no issue if make the bond (contentOffset listener) equals to nil in right place.

@zdnk
Copy link

zdnk commented Apr 10, 2015

I am currently in the same situation. I have couple KVO observers, then values mapped to viewModel and those dynamics bound to UIViews. This all is wrapped in DynamicArray which when cleared (removeAll) causes crash

'An instance 0x1706690c0 of class BETPossibilityModel was deallocated while key value observers were still registered with it. Current observation info: (NSKeyValueObservationInfo 0x174458e10) (
)NSKeyValueObservance 0x1740d5540: Observer: 0x1740d54d0, Key path: name, Options: (New: YES, Old: NO, Prior: NO) Context: 0x0, Property: 0x174458d80)

How to fix this? :O

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants