Coalesced object change notifications

From EOFWiki
Jump to: navigation, search

Code

#import <Foundation/Foundation.h>
#import <EOControl/EOControl.h>
 
 
@interface PermanentGlobalID : EOTemporaryGlobalID
@end
 
@implementation PermanentGlobalID : EOTemporaryGlobalID
 
- (BOOL) isTemporary
{
   return( NO);
}
 
@end
 
 
@interface NotificationLogger : NSObject
@end
 
@implementation NotificationLogger
 
+ (void) logNotification:(NSNotification *) notification
{
   NSLog( @"%@", notification);
}
@end
 
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool   *pool;
   EOEditingContext    *context;
   NSMutableString     *obj;
   NSMutableString     *obj2;
   EOGlobalID          *gid;
 
   pool = [NSAutoreleasePool new];
 
   [[NSNotificationCenter defaultCenter] addObserver:[NotificationLogger class]
                                            selector:@selector( logNotification:)
                                                name:nil
                                              object:nil];
 
   context = [[EOEditingContext new] autorelease];
 
   obj = [[@"VfL Bochum 1848" mutableCopy] autorelease];
   gid = [[PermanentGlobalID new] autorelease];
   [context recordObject:obj
                globalID:gid];
 
   [obj willChange];
   [obj appendString:@"!"];
 
   obj2 = [[@"Mulle kybernetiK" mutableCopy] autorelease];
   [context insertObject:obj2];
   [obj2 willChange];
   [obj2 appendString:@"!"];
 
   NSLog( @"A)");
   [context processRecentChanges];
 
   [pool release];
   return( 0);
}

Comment

This time it is avoided to call processRecentChanges implicitly by the NSLog code. The NSNotifications are manually triggered and as can be seen, all edits have been coalesced into one NSNotification.

Output

A)
NSConcreteNotification 0x114d60 {name = EOObjectsChangedInStoreNotification; object = <EOEditingContext: 0x1063c0>; userInfo = {
    deleted = ();
    inserted = ( <EOTemporaryGlobalID 0x115e10 (hash=0x9792A1F4)>);
    invalidated = ();
    updated = ( <PermanentGlobalID 0x1093e0 (hash=0x9318217B)>,
                <EOTemporaryGlobalID 0x115e10 (hash=0x9792A1F4)>);
   }
}
2008-09-21 14:35:35.687 tracking-notifications-colaesced[12100:813] NSConcreteNotification 0x114d60 {name = EOObjectsChangedInEditingContextNotification; object = <EOEditingContext: 0x1063c0>; userInfo = {
    deleted = ();
    inserted = ( "Mulle kybernetiK!" );
    invalidated = ();
    updated = ( "VfL Bochum 1848!",
                "Mulle kybernetiK!"
    		 );
   }
}
Personal tools