Re: How to import data in Core Data app?
Re: How to import data in Core Data app?
- Subject: Re: How to import data in Core Data app?
- From: "Arthur C." <email@hidden>
- Date: Fri, 16 Mar 2007 17:53:26 +0100
On Mar 1, 2007, at 9:12 AM, Arthur C. wrote:
The question is how to handle this using Core Data. In the Core Data
Programming guide there is a topic on 'importing legacy data', but that
only covers the merging / removing duplicates issues. What I would like
to know is how to save things to file (another persistent store?), and
retrieve them again. That is, using as much of Core Data as possible, as
reading/writing to a file can of course be done without the framework.
It's entirely up to you how you read and write the data.
In general, to read in:
Create an in-memory store.
Read in your objects, creating for each an instance of the relevant
entity.
(this is where the article in the Programming Guide is useful)
Re-create any relatonships (you'll have to give your objects IDs...)
to write out:
Fetch against each entity in turn
Write out each instance of each entity (make sure you write out object
IDs for relationships)
mmalc
Okay, in this approach one would have to do the file I/O 'by hand'. In
particular, it's not easy to write to an XML file in the same way Core Data
does.
Isn't it possible to copy the (XML) persistent store file to the other user,
who then adds it as a second (read-only) persistent store? This can be done
using:
NSManagedObjectContext *moc = [self managedObjectContext];
NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber
numberWithBool:1] forKey: NSReadOnlyPersistentStoreOption];
id importStore = [psc addPersistentStoreWithType:NSXMLStoreType
configuration:nil URL: myURL options:options error:&error];
NSFetchRequest *myRequest = [[[NSFetchRequest alloc] init] autorelease];
[myRequest setAffectedStores:[NSArray arrayWithObject: importStore]];
[myRequest setEntity: [[[self managedObjectModel] entitiesByName]
valueForKey: @"Person"] ];
NSArray *importRecords = [moc executeFetchRequest: myRequest error:
error];
The problem is that Core Data will now attempt to save the fetched objects
to the same persistent store, which is read-only. They should be included in
the first (standard) persistent store instead.
Questions:
- Is there a faster way to assign them to the first persistent store than
inserting a new object for each fetched object, and copying the values for
all keys? Directly assigning the fetched objects to another persistent store
gives an error.
- Is this a valid approach anyway? Or are there good reasons to insist on
the manual file I/O - in-memory store approach?
Thanks in advance,
Arthur C.
_________________________________________________________________
Play online games with your friends with Messenger
http://www.join.msn.com/messenger/overview
_______________________________________________
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