Skip to content

Instantly share code, notes, and snippets.

@atomicbird
Created January 30, 2012 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomicbird/1706770 to your computer and use it in GitHub Desktop.
Save atomicbird/1706770 to your computer and use it in GitHub Desktop.
NSManagedObjectContext *theCreatingMOC = theChildMOC;
[theCreatingMOC performBlockAndWait:^{
// Insert some test data...
NSManagedObject *thePerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:theCreatingMOC];
[thePerson setValue:@"Steve Jobs" forKey:@"name"];
NSManagedObject *theCompany = [NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:theCreatingMOC];
[theCompany setValue:@"Apple" forKey:@"name"];
[theCompany setValue:[NSSet setWithObject:thePerson] forKey:@"employees"];
// And save it...
NSError *theError = NULL;
[theChildMOC save:&theError];
NSLog(@"MOC Saved (%@)", theError);
// Save our main MOC (if the inserts happen in a child MOC, saving the child MOC bubbles the changes up the main MOC, so we now have to save the main MOC)
[theMainMOC performBlockAndWait:^{
NSError *theError = NULL;
[theMainMOC save:&theError];
NSLog(@"MOC Saved (%@)", theError);
}];
// Store a object URL so we can do our magic in whatever MOC we want...
theCompanyURL = theCompany.objectID.URIRepresentation;
NSLog(@"%@", theCompanyURL);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment