PDA

View Full Version : Cocoa "Property Listeners"?




Littleodie914
Jul 23, 2008, 09:27 AM
Hey guys, I've got an interesting question for you. I've got a "last modified" attribute on all the objects stored in my data model, as I'm attempting to build a synchronization engine for the first time.

There's a method at the top-level of my data model abstraction, "modify", which updates the last modified timestamp to the current time.

But, all of my values are stored as properties, and there are *lots* of them. One way to go about this would be to manually implement *all* of the setters, changing something from:

- (void)setTitle:(NSString *)newTitle {
if (newTitle != title) {
[title release];
title = [newTitle retain];
}
}

To something like:

- (void)setTitle:(NSString *)newTitle {
if (newTitle != title) {
[title release];
title = [newTitle retain];
[self modify];
}
}

But rather than doing that for all my properties, it would be great if I could do something like: (pseudo-ish)

- (void)propertyValueWillChange:(NSString *)propertyName toValue:(id)newValue {
[super propertyValueWillChange:propertyName toValue:newValue];
[self modify];
}

Hope that makes sense! Is there any easy way to do this? :)



AussieSusan
Jul 23, 2008, 06:00 PM
How does what you are asking for differ from Key Value Observing. In your case where you are using core data, you may need to implement your own classes but they should be very straight forward and you can use the @property keywords to set everything up for you.

Susan

Sbrocket
Jul 24, 2008, 12:34 AM
KVO or Key Value Observing is what you're looking for. The Apple documentation on it is here:

http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

Basically, you can register a target to receive notifications when a registered key changes. The two methods you want to look at are -[NSObject addObserver:forKeyPath:options:context] and -[NSObject observeValueForKeyPath:ofObject:change:context:], the first of which you use to register an object for notifications and the second you implement to observe the changes. It would be a good idea to read through that article, though.

Littleodie914
Jul 24, 2008, 05:57 PM
Ah great, that worked like a charm. :) I have used KVO before, but only with Core Data. I didn't know it could be implemented so flexibly.

One more question though, just to be picky. :p

The specific object that I wanted to use KVO with has many properties (15+) and while adding an observer for all of them would work, is there any way to add an observer to monitor *any* changes in the class? That would:

1) Eliminate a huge block of addObserver: methods.

2) Provide flexibility in the model, so that whenever any new properties were added, it wouldn't be necessary to add more of those methods.

Now that I think about it, is there an Objective-C version of reflection, like in Java? So that rather than adding a big generic observer, you could just loop through all of the properties in the class and add observers that way?