Tracking object creation and removal

From EOFWiki
Jump to: navigation, search

Contents

Code

#import <Foundation/Foundation.h>
#import <EOControl/EOControl.h>
 
 
int main (int argc, const char * argv[])
{
   NSAutoreleasePool   *pool;
   EOEditingContext    *context;
   NSString            *obj;
   NSString            *obj2;
   EOGlobalID          *gid;
 
   pool = [NSAutoreleasePool new];
 
   context = [[EOEditingContext new] autorelease];
 
   obj = @"VfL Bochum 1848";
   gid = [[EOTemporaryGlobalID new] autorelease];
 
   NSLog( @"A) %d insertedObjects, %d registeredObjects, %d deletedObjects",
         [[context insertedObjects] count], [[context registeredObjects] count],  [[context deletedObjects] count]);
 
   [context recordObject:obj
                globalID:gid];
 
   NSLog( @"B) %d insertedObjects, %d registeredObjects, %d deletedObjects",
         [[context insertedObjects] count], [[context registeredObjects] count],  [[context deletedObjects] count]);
 
   obj2 = @"Mulle kybernetiK";
   [context insertObject:obj2];
 
   NSLog( @"C) %d insertedObjects, %d registeredObjects, %d deletedObjects",
         [[context insertedObjects] count], [[context registeredObjects] count],  [[context deletedObjects] count]);
   NSLog( @"D) GlobalIDForObject %@ = %@", obj, [context globalIDForObject:obj]);
 
   [context deleteObject:obj2];
   NSLog( @"E) %d insertedObjects, %d registeredObjects, %d deletedObjects",
         [[context insertedObjects] count], [[context registeredObjects] count],  [[context deletedObjects] count]);
 
   [context deleteObject:obj];
   NSLog( @"F) %d insertedObjects, %d registeredObjects, %d deletedObjects",
         [[context insertedObjects] count], [[context registeredObjects] count],  [[context deletedObjects] count]);
 
   [pool release];
   return( 0);
}

Comment

See Registering and unregistering objects before continuing.

An EOEditingContext is created, which is intially empty (see Output A). As before an object is recorded into the EOEditingContext like in Registering and unregistering objects (Output C). Then freshly created object, in this case another NSString is placed into the EOEditingContext (Output E). Finally both objects are deleted again from the EOEditingContext (Output F and G).

When inserting the EOEditingContext provides an EOTemporaryGlobalID for the object and registers this object for you (Output D+E). The inserted object shows up in the list of insertedObjects, whereas the only recorded object does not.

Output

A) 0 insertedObjects, 0 registeredObjects, 0 deletedObjects
B) 0 insertedObjects, 1 registeredObjects, 0 deletedObjects
C) GlobalIDForObject VfL Bochum 1848 = <EOTemporaryGlobalID 0x1085b0 (hash=0x1F3226B3)>
D) 1 insertedObjects, 2 registeredObjects, 0 deletedObjects
E) GlobalIDForObject Mulle kybernetiK = <EOTemporaryGlobalID 0x1143d0 (hash=0x53DC7EF4)>
F) 0 insertedObjects, 1 registeredObjects, 0 deletedObjects
G) 0 insertedObjects, 0 registeredObjects, 0 deletedObjects

Note

Inserted objects never show up in deleted objects, only registered objects will. Except! if they were registered with an EOGlobalID that says YES to -isTemporary (as any EOTemporaryGlobalID instance predictably will). In this case the object is just forgotten. See Tracking object creation and removal with permanent EOGlobalID for the subtle difference.

Personal tools