NSManagedObject

Basic

 * http://stackoverflow.com/questions/2842258/unit-testing-model-classes-that-inherit-from-nsmanagedobject
 * http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/Articles/cdCreateMOs.html
 * http://importantshock.wordpress.com/2006/12/27/nsmanagedobject-better-than-sliced-bread-yes/

Advanced
NSURL *uri = [[myObject objectID] URIRepresentation]; ... NSData *uriData = [NSKeyedArchiver archivedDataWithRootObject:uri]; NSPersistentStoreCoordinator:
 * http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html

-(NSManagedObjectID *)managedObjectIDForURIRepresentation:(NSURL *)uri; -(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID; @implementation NSManagedObjectContext (FetchedObjectFromURI) - (NSManagedObject *)objectWithURI:(NSURL *)uri {   NSManagedObjectID *objectID = [[self persistentStoreCoordinator]           managedObjectIDForURIRepresentation:uri];    if (!objectID)    {        return nil;    }    NSManagedObject *objectForID = [self objectWithID:objectID];    if (![objectForID isFault])    {        return objectForID;    }

NSFetchRequest *request = [NSFetchRequest alloc] init] autorelease];   [request setEntity:[objectID entity; // Equivalent to   // predicate = [NSPredicate predicateWithFormat:@"SELF = %@", objectForID]; NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForEvaluatedObject] rightExpression: [NSExpression expressionForConstantValue:objectForID] modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0]; [request setPredicate:predicate];

NSArray *results = [self executeFetchRequest:request error:nil]; if ([results count] > 0 ) {       return [results objectAtIndex:0]; }

return nil; } @end - (void)setup { DLog(@"Registering for NSManagedObjectContext notifications"); NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(test:) name:NSManagedObjectContextWillSaveNotification object:nil]; [nc addObserver:self selector:@selector(test:) name:NSManagedObjectContextDidSaveNotification object:nil]; [nc addObserver:self selector:@selector(test:) name:NSManagedObjectContextObjectsDidChangeNotification object:nil]; }
 * http://stackoverflow.com/questions/2401476/missing-core-data-notifications

- (void)test:(NSNotification *)notif { DLog(@"Test callback"); }

willSave "Invoked automatically by the Core Data framework when the receiver’s managed object context is saved." - (void)willSave This method can have “side effects” on persistent values. You can use it to, for example, compute persistent values from other transient or scratchpad values.
 * http://developer.apple.com/mac/library/documentation/cocoa/reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/awakeFromInsert

If you want to update a persistent property value, you should typically test for equality of any new value with the existing value before making a change. If you change property values using standard accessor methods, Core Data will observe the resultant change notification and so invoke willSave again before saving the object’s managed object context. If you continue to modify a value in willSave, willSave will continue to be called until your program crashes.

For example, if you set a last-modified timestamp, you should check whether either you previously set it in the same save operation, or that the existing timestamp is not less than a small delta from the current time. Typically it’s better to calculate the timestamp once for all the objects being saved (for example, in response to an NSManagedObjectContextWillSaveNotification).

If you change property values using primitive accessors, you avoid the possibility of infinite recursion, but Core Data will not notice the change you make.

Note that the sense of “save” in the method name is that of a database commit statement and so applies to deletions as well as to updates to objects. For subclasses, this method is therefore an appropriate locus for code to be executed when an object deleted as well as “saved to disk.” You can find out if an object is marked for deletion with isDeleted.