On Aug 13, 2014, at 11:31 , Luc Van Bogaert <luc.van.boga...@me.com> wrote:

> My question now is , where I should remove MyView as an observer with 
> 'self.model removeObserver: self forKey: @"property"...' '?
> I tried to do this in 'dealloc', but it seems that from time to time my model 
> object is pulled right from under my when I close the document window, 
> resulting in an error stating that MyModel was deallocated while it still had 
> observers attached to it.

Yes, this behavior is a consequence of the fact that the view’s “model” 
property is weak, and therefore not KVO compliant. (It can change to nil 
without anyone getting notified.)

One way to handle this is to implement the window delegate method 
‘windowWillClose:’ in your window controller, and use that to inform the view 
that it should stop observing the model.

Unfortunately, there’s one other case you probably need to handle — what 
happens when the model is deallocated for some other reason. It’s possible that 
this isn’t possible in your app, but consider what happens if the user reverts 
the document, and the document object handles that by re-reading the document 
file and creating a new model. (I *think* that’s what happens if you don’t have 
any revert-handling code of your own.)

In that case , the view’s weak “model” property will change to nil, which isn’t 
very useful.

The upshot of this is that you’ll likely have a model-monitoring slab of code 
in your window controller, which is responsible for keeping the view up to 
date. Then, you’d probably use this mechanism to get the model reference to the 
view initially, rather than having the custom initializer. Or, you’d change the 
view to observe a “model” property of the window controller, which itself is 
derived from the document’s “model” property.

But the details will vary depending on the details of your app.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to