I have a NSPersistentDocument
with a given Core Data model, etc.
I have a file, created by this document, let's say it its preload.xml. It "contains" several NSManagedObject
s.
I would like to load these objects in all my new documents, so that when I create a new document, the new document automatically "has" the objects "living" in preload.xml. So far, here is what I did :
I copied
preload.xml
in my project.In the
initWithType:error:
method (the method called when a new document is created), have the following code :NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"preload" ofType:@"xml"]]; NSError* err = nil; [self readFromURL:preloadURL ofType:@"xml" error:&err] ;
This doesn't work because, when I try afterwards to save my document to, let's say myNewDoc.xml
, this file is empty but all my new data is saved to preload.xml
.
I am wondering if I need to create a new store
or context
or storeCoordinator
or something else. I never dealt with such objects since I always used NSPersistentDocument
.
readFromURL:ofType:error:
? If so, what does your version look like? – Tom HarringtonreadFromURL
but it does not work. I did something else, since : create a newstore
(associated to my preload.xml), a newstoreCoord
and a newMOC
. Then, I had a clone my objects between the two MOC, which is not so great... But it seems to work. – ColasreadFromURL:ofType:error:
code, and since you didn't include that it's hard to say what might be wrong with it. – Tom HarringtonreadFromURL
!!! I meant : yes, I usedreadFromURL
. Sorry for the misunderstanding :/ – Colas