Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
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:

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

To something like:

Code:
- (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)

Code:
- (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

macrumors member
May 29, 2006
54
0
Melbourne, Australia
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

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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:eek:ptions:context] and -[NSObject observeValueForKeyPath:eek:fObject: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

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.