Tracking object changes
Contents |
Code
#import <Foundation/Foundation.h> #import <EOControl/EOControl.h> @interface PermanentGlobalID : EOTemporaryGlobalID @end @implementation PermanentGlobalID : EOTemporaryGlobalID - (BOOL) isTemporary { return( NO); } @end int main (int argc, const char * argv[]) { NSAutoreleasePool *pool; EOEditingContext *context; NSMutableString *obj; EOGlobalID *gid; pool = [NSAutoreleasePool new]; context = [[EOEditingContext new] autorelease]; obj = [[@"VfL Bochum 1848" mutableCopy] autorelease]; gid = [[PermanentGlobalID new] autorelease]; [context recordObject:obj globalID:gid]; NSLog( @"A) %d updatedObjects, %d registeredObjects", [[context updatedObjects] count], [[context registeredObjects] count]); [obj willChange]; [obj appendString:@"!"]; NSLog( @"B) %d updatedObjects, %d registeredObjects", [[context updatedObjects] count], [[context registeredObjects] count]); [pool release]; return( 0); }
Comment
To allow the EOEditingContext to track changes the -willChange method of the EOEnterpriseObject protocol must be used. This method is added by EOF as a category onto NSObject and therefore available for (virtually) all objects.
-willChange will determine the EOEditingContext the object resides in and notify this EOEditingContext of the upcoming change. It is an error to call -willChange after the manipulation as we will see in a later tutorial (involving undo and snapshotting).
Output
A) 0 updatedObjects, 1 registeredObjects B) 1 updatedObjects, 1 registeredObjects
Note
Inserted objects do not shows up in the list of updatedObjects, as they are already known to be inserted. Customary for EOF programs is to write the -willChange method call into the accessors of the class.
See Tracking object changes with an accessor for an example of both these issues.