There is something I am missing in my understanding of UICollectionViewCell lifecycle.
When UICollectionViewCell is created and configured I add observer on one of its properties
func setCellDetails(someDetails:SomeObject)
{
...
self.someProperty.addObserver(self, forKeyPath: "objectProperty", options: .New, context: nil)
...
}
I remove the observer on prepareForReuse
override func prepareForReuse()
{
super.prepareForReuse()
self.someProperty.removeObserver(self, forKeyPath: "objectProperty")
}
But then when I am jumping between tabs of the application and influence the objectProperty
the cell is not effected.
I debugged the code and found that when I am changing the tabs, prepareForReuse of the cell is called so the observer is removed and it never added back because cell setup function is not called. So maybe I should add or remove the observer in other functions?
I tried to put the removeObserver
in deinit
and it crashes with the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x12eb89dd0 of class ObservedObject was deallocated while key value observers were still registered with it. Current observation info: NSKeyValueObservationInfo NSKeyValueObservance Observer: .. Key path: objectProperty
I thought maybe not to put removeObserver in anyplace. It produced the same error.
What should I do? Where should I put it?