I can't find a clear description of these steps in Apple docs...
Now, I want to stop doing that heavy XML parsing process on device and directly include a Store containing the required data.
I have some questions regarding this :
Thanks for your hints. URL or other SO questions would be really appreciate !
Kheraud
You can include the store file (sqlite db most of the time) in your app. Then in your app delegate edit the persistentStoreCoordinator getter merhod :
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataStore.sqlite"];
// Check if the store exists in.
if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
// copy the payload to the store location.
NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"YourPayload" ofType:@"sqlite"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:&error];
if (error){
NSLog(@"Error copying payload: %@", error);
}
}
NSError *error = nil;
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
That's all there really is to it.
What you're currently doing (populating at first launch) is the "recommended" way of populating a Core Data store. Although its a bit hackish, you could however seed the on-device database as follows:
~/Library/Application Support/iPhone Simulator/4.3/Applications/335567A0-760D-48AF-BC05-7F0D9BD085B6/<app-name>.app/
)application:didFinishLaunchingWithOptions:
method so that on first launch, it copies the database from the read-only resources directory to the app's documents directory. Of course you need to do this before initializing Core Data.Depending on exactly what you're storing in your database, you may however discover big- vs. little-endianness issues, or other incompatibilities. To make the approach a bit safer, you could dump the simulator database (splite3 databasefile .dump >dumpfile
) on your Mac, then include the dumpfile in your project (as above), and slurp the dump in your app on first launch (reading it line-by-line, and handing the sql statements to the sqlite API).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With