Registering and unregistering objects

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;
   EOGlobalID          *gid;
 
   pool = [NSAutoreleasePool new];
 
   context = [[EOEditingContext new] autorelease];
 
   obj = @"VfL Bochum 1848";
   gid = [[EOTemporaryGlobalID new] autorelease];
 
   NSLog( @"A) %d registeredObjects", [[context registeredObjects] count]);
 
   [context recordObject:obj
                globalID:gid];
 
   NSLog( @"B) %d registeredObjects", [[context registeredObjects] count]);
   NSLog( @"C) ObjectForGlobalID %@ = %@", gid, [context objectForGlobalID:gid]);
   NSLog( @"D) GlobalIDForObject %@ = %@", obj, [context globalIDForObject:obj]);
 
   [context forgetObject:obj];
 
   NSLog( @"E) %d registeredObjects", [[context registeredObjects] count]);
 
   [pool release];
   return( 0);
}

Comment

On the top of this little Foundation tool, the necessary Framework headers are imported. Then main() sets up the customary NSAutoreleasePool for "garbage collection" and then proceeds to do the actual EOF stuff.

An EOEditingContext is created, which is intially empty (see Output A). An object, in this case a NSString is placed into the EOEditingContext (Output B). Finally the object is removed again from the EOEditingContext (Output E).

The object needs a unique EOGlobalID as a reference and the EOEditingContext is not providing it itself. The EOGlobalID is pretty much the same idea as an URL or an UUID or a GUID just in EOF terminology (and subclassable). We use a EOTemporaryGlobalID global ID to generate us a unique identifier. Afterwards the object can be retrieved by it's EOGlobalID and vice versa.

Output

A) 0 registeredObjects
B) 1 registeredObjects
C) ObjectForGlobalID <EOTemporaryGlobalID 0x1085a0 (hash=0xE5F41685)> = VfL Bochum 1848
D) GlobalIDForObject VfL Bochum 1848 = <EOTemporaryGlobalID 0x1085a0 (hash=0xE5F41685)>
E) 0 registeredObjects


Note

In most EOF applications, the registration of objects is done by the EOObjectStore for you during fetching and faulting.

Personal tools