Tracking object changes with an accessor

From EOFWiki
Jump to: navigation, search

Contents

Code

#import <Foundation/Foundation.h>
#import <EOControl/EOControl.h>
 
 
@interface PermanentGlobalID : EOTemporaryGlobalID
@end
 
@implementation PermanentGlobalID : EOTemporaryGlobalID
 
- (BOOL) isTemporary
{
   return( NO);
}
 
@end
 
 
@interface MutableDictionary : NSMutableDictionary
{
   NSMutableDictionary   *dictionary_;
}
 
@end
 
 
@implementation MutableDictionary : NSMutableDictionary
 
- (id) init
{
   dictionary_ = [NSMutableDictionary new];
   return( self);
}
 
- (void) dealloc
{
   [dictionary_ release];
   [super dealloc];
}
 
 
- (void) setObject:(id) obj
            forKey:(id) key
{
   [self willChange];
   [dictionary_ setObject:obj
                   forKey:key];
}
 
@end
 
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool   *pool;
   EOEditingContext    *context;
   MutableDictionary   *obj;
   MutableDictionary   *obj2;
   EOGlobalID          *gid;
 
   pool = [NSAutoreleasePool new];
 
   context = [[EOEditingContext new] autorelease];
 
   obj = [[MutableDictionary new] autorelease];
   gid = [[PermanentGlobalID new] autorelease];
   [context recordObject:obj
                globalID:gid];
 
   NSLog( @"A) %d updatedObjects, %d registeredObjects",
         [[context updatedObjects] count], [[context registeredObjects] count]);
 
   [obj setObject:@"VfL Bochum 1848"
           forKey:@"name"];
 
   NSLog( @"B) %d updatedObjects, %d registeredObjects",
         [[context updatedObjects] count], [[context registeredObjects] count]);
 
   obj2 = [[MutableDictionary new] autorelease];
   [context insertObject:obj2];
 
   NSLog( @"C) %d updatedObjects, %d registeredObjects",
         [[context updatedObjects] count], [[context registeredObjects] count]);
 
   [obj2 setObject:@"VfL Bochum 1848"
            forKey:@"name"];
 
   NSLog( @"D) %d updatedObjects, %d registeredObjects",
         [[context updatedObjects] count], [[context registeredObjects] count]);
 
   [pool release];
   return( 0);
}

Comment

To enable change tracking a crucial part of the EOEnterpriseObject protocol must be implemented. This is the -willChange method that will notify the EOEditingContext that the contents of the object will change.

Notice that the inserted object does not shows up in the list of updatedObjects, as it is already known to be inserted.

Output

A) 0 updatedObjects, 1 registeredObjects
B) 1 updatedObjects, 1 registeredObjects
C) 1 updatedObjects, 2 registeredObjects
D) 1 updatedObjects, 2 registeredObjects

Note

This piece of code could be added to MutableDictionary for brownie points:

// forward functionality to NSMutableDictionary
- (NSMethodSignature *) methodSignatureForSelector:(SEL) sel
{
   return( [dictionary_ methodSignatureForSelector:sel]);
}
 
 
- (void) forwardInvocation:(NSInvocation *) invocation
{
   SEL  sel;
 
   sel = [invocation selector];
   if (! [dictionary_ respondsToSelector:sel])
   {
      // does this ever happen ? 
      [self doesNotRecognizeSelector:sel];
      return;
   }
 
   [invocation invokeWithTarget:dictionary_];
}
Personal tools