How can I observe the creation / destruction of an instance of an object?

KVC / KVO amazes me. Super powerful. However, there is one problem. I try to be faithful to MVC eto, but I see no way to use a watch pattern to control the allocation or release of an instance of the Objective-C class.

This is really important because I have a model with a fine-grained internal message that I want to watch from the controller (or delegate). Some kind of obstacle for me is that I don’t see how external to the model I can remove the observer for the subcomponent, which should be freed if the controller does not know about the internal logic of the model that could break encapsulation.

Can someone suggest an approach for this scenario.

Thanks Doug

+4
source share
3 answers

Doug - there is really not enough information in your description to know what you are doing, and how best (or if at all) apply KVO to this problem.

KVO is all about objects to monitor objects. Usually, it doesn’t matter to you when they are created or destroyed, unless you must stop observing them before they are destroyed.

Instead, you should start and stop observing objects when these objects become of interest to you. Consider a graphic drawing package in which a document has an ordered array of shapes, and you are interested in observing the backgroundColor property of each shape.

We will not try to observe the instance and the release of the Shape instance, but instead we observe the “shape” property in the document. Through this observer, we can determine when the form is added or removed from the document. When the form is added to the document, we begin to observe it. When it is removed from the document, we stop observing it. (Note that it can be removed from the document, but not freed if it is on the cancel stack, etc.)

In the object graph for your model, in order to use KVO, you will want to add and remove objects from the object graph using the KVO-compatible method so that you can observe mutations in the relationship and in this observer start and stop the property of observers for related objects.

+6
source

I think you will have to post notifications yourself if you are not using something like CoreData. If you use CoreData, NSManagedObject (the root class of all stored CoreData objects) has a -awakeFromInsert method, which is called after the object has been created and inserted into the ManagedObjectContext.

As far as destruction is concerned, you can simply post a notification right when you enter the -dealloc method.

+1
source

I'm not sure exactly what you are trying to achieve, so a little more explanation will be good.

If you just want to remove the observer before the detected object is released, then do not worry, because KVO will handle this. Even if you use notifications, this will not cause a problem, you simply will not receive any notifications from the object.

If you are trying to observe multiple objects (for example, an array of widgets) and want to know when an object has been added or removed, KVO can handle this too. You just need to make the array a key on your model object and observe it with KVO. You must also modify the array using the KVO-compatible method (for example, mutableArrayForKey :, or use your own willChangeValueForKey and didChangeValueForKey).

0
source

All Articles