Re: How to load from templates like in iWork using CoreData?
Re: How to load from templates like in iWork using CoreData?
- Subject: Re: How to load from templates like in iWork using CoreData?
- From: "E. Wing" <email@hidden>
- Date: Wed, 12 Oct 2005 19:51:08 -0700
I think I managed to get most of this working. I'm posting my
solution in case others want to see this.
The problem with the UI not being updated was my stupid error that
was totally unrelated to this.
As for the dictionary ReadOnly option, I still don't know why that
was creating an error. I still could use a feedback on that one.
But here's the code I'm now running:
NSPersistentStoreCoordinator* persistent_store_coordinator = [[self
managedObjectContext] persistentStoreCoordinator];
template_store = [persistent_store_coordinator
addPersistentStoreWithType: NSXMLStoreType configuration:nil URL:
template_url options: nil error: &error];
[persistent_store_coordinator migratePersistentStore: template_store
toURL: nil options: nil withType: NSInMemoryStoreType error: &error];
I pass in nil for the options to avoid the error for the
addPersistentStore. I realized I should not be making the
InMemoryStore read-only so I pass in nil there as well.
For the document saving, I found
writeToURL:ofType:forSaveOperation:originalContentsURL:error: in
NSDocument was a place I could try overriding. This is where I need to
migrate the store one more time from InMemory to an actual file.
However, I discovered I actually have 3 cases: an existing document, a
new document from a template, and a new blank document. If I run the
migration code on an existing document, I get crash. And if I have a
new blank document,I actually don't have any persistent stores to
migrate.
So I have two ugly checks which I can only hope will hold up in all
cases. First I check if [self fileURL] is nil to decide if it's a new
document. Then I need to check if the count of persistentStores > 0.
So my code looks like this:
- (BOOL)writeToURL:(NSURL *)absoluteURL
ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation
originalContentsURL:(NSURL *)absoluteOriginalContentsURL
error:(NSError **)error
{
if ([self fileURL] != nil)
{
NSLog(@"Found fileURL: %@", [self fileURL]);
}
else
{
NSPersistentStoreCoordinator* persistent_store_coordinator = [[self
managedObjectContext] persistentStoreCoordinator];
persistent_stores = [persistent_store_coordinator persistentStores];
if(nil != persistent_stores)
{
if([persistent_stores count] > 0)
{
id migrate_store;
id store;
store = [persistent_stores objectAtIndex: 0];
migrate_store = [persistent_store_coordinator
migratePersistentStore: store toURL: absoluteURL options: nil
withType: typeName error: error];
}
}
}
return [super writeToURL:absoluteURL
ofType:typeName
forSaveOperation:saveOperation
originalContentsURL:absoluteOriginalContentsURL
error:error];
}
Anyway, if anybody can tell me why the read-only option is failing for
me, or spots anything that can be done better here, I would appreciate
the feedback.
Thanks,
Eric
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden