Exporting objects to persistent store
Exporting objects to persistent store
- Subject: Exporting objects to persistent store
- From: Drew McCormack <email@hidden>
- Date: Thu, 25 Jan 2007 14:53:11 +0100
I have a CoreData app (non-document based) which has a primary store
in App Support. I want to allow the user to select objects in the UI,
and export those to a file. The managed object model of the main
store and external file are the same. So it is similar to a
migration, but without any change of model.
I have based my solution on CoreRecipes. I follow these steps:
1) Create a persistent store for the export file
2) Create a managed object context for the objects being exported
3) Walk the object tree copying objects and relationships from
primary store to export store
4) Save export context
5) Remove persistent store from PS coordinator
6) Release export managed object context
The problems occur at 5 and 6. When the persistent store and/or the
context are removed, I get exceptions like this:
2007-01-25 14:16:12.857 Mental Case[12427] *** NSRunLoop ignoring
exception 'The NSManagedObject with ID:0xd52d3d0 <x-coredata://
9D5CB268-5591-4305-B756-DE317EEEE059/KnowledgeItem/p104> has been
invalidated.' that raised during posting of delayed perform with
target be15b60 and selector 'cleanUpExportPersistenceStack'
I have tried postponing this clean up to the next run loop, and often
it works, but occasionally it doesn't. I think it has to do with some
KVO observation code I have (addObserver:forKeyPath:...), but surely
it can be made to work without me pulling all this code out.
The question in a nutshell is: How do you break down a temporary
NSManagedObjectContext and/or persistent store? Or do I just have to
let them remain in existence until the program quits?
Drew
---------------------------------------------------------
Drew McCormack
www.macanics.net
www.macresearch.org
Here is the main driver routine, in case it helps:
-(BOOL)exportObjects:(id)objects toFile:(NSString *)path error:
(NSError **)error {
BOOL returnValue = YES;
*error = nil;
// Process changes
[ManagedObjectContext() processPendingChanges];
if ( ![ManagedObjectContext() save:error] ) return NO;
[[ManagedObjectContext() undoManager] disableUndoRegistration];
// Setup export store
NSURL *newStoreURL = [NSURL fileURLWithPath:path];
NSPersistentStoreCoordinator *coordinator = [ManagedObjectContext
() persistentStoreCoordinator];
exportStore = [coordinator
addPersistentStoreWithType:NSXMLStoreType configuration:nil
URL:newStoreURL options:nil error:error];
if ( exportStore == nil ) return NO;
// Create new managed object context to handle exported objects
exportContext = [[NSManagedObjectContext alloc] init];
[exportContext setPersistentStoreCoordinator:coordinator];
[[exportContext undoManager] disableUndoRegistration];
// Copy objects to export context
NSEnumerator *en = [objects objectEnumerator];
MCManagedObject *obj;
while ( obj = [en nextObject] ) {
[obj copyToContext:exportContext andStore:exportStore];
}
// Save to export store
if ( NO == [exportContext save:error] ) {
NSLog(@"Save error: %@", *error);
returnValue = NO;
}
[[ManagedObjectContext() undoManager] enableUndoRegistration];
// Remove context and store
if ( ![[ManagedObjectContext() persistentStoreCoordinator]
removePersistentStore:exportStore error:error] ) {
NSLog(@"Failed to remove persistent store: %@", *error);
}
[exportContext release];
return returnValue;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden