 |
 |
Loading SQL vs. XML Core Data Stores
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
I am trying to create a small application that can read in SQL-based
core data stores from another application. To get the basics working
I made a test application that can load the core data store from the
Core Recipes example. If I save the data in XML (the default in the
CR example) my application can read it in and fetch entities without
any issues. If I have CR save the data as an SQL file, and I try to
have my application read the data in, any fetch requests return an
empty array. Am I missing something obvious that I need to do for
the SQL case? Has anyone else run into this issue?
Thanks for your time,
Ian
(this is a cross post on Cocoa-Dev list from which I could not get a reply :-()
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
Well, you do have to adjust the code a little bit for the different formats -- check the Core Data docs.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Thanks for the reply Mithras. I looked through the documentation more this morning and didn't find anything specific about SQLite accept to let the persistent store coordinator know which type of store you are adding to it. Below is my code if you want to have a look. Thanks for your help thus far, I really appreciate it.
//load the model from app resources
NSArray *bundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:bundles];
//add model to store coordinator
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
//add persistent store w/appropriate type from the URL
NSError *error;
if (![store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){
[[NSApplication sharedApplication] presentError:error];
}
//create context and set the store
NSManagedObjectContext *managedObjectContext = nil;
if (store != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: store];
}
//try a fetch request
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
this array is always empty :-( even though I know there are "MyEntities"
NSArray *entities = [managedObjectContext executeFetchRequest:request error:&error];
//do cleanup
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
Dumb question, but are you setting the URL appropriately?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Good to check that, but yeah, the URL is valid and if use it to load an XML store, it works fine. Any other thoughts?
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |