Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to pre populate core data?

I've been creating a list app and backing it with core data.

I would like to have a default list of say 10 airport's items, so that the user doesn't have to start from scratch.

Is there any way to do this?

Any help is appreciated. Thanks in advance.

like image 548
Tanner Avatar asked Feb 09 '10 15:02

Tanner


People also ask

Is Core Data == SQLite?

Core Data and SQLite are solutions to different problems. Core Data can use SQLite as its persistent store, but the framework itself is not a database. Core Data is not a database. Core Data is a framework for managing an object graph.


2 Answers

Here's the best way (and doesn't require SQL knowledge):
Create a quick Core Data iPhone app (Or even Mac app) using the same object model as your List app. Write a few lines of code to save the default managed objects you want to the store. Then, run that app in the simulator. Now, go to ~/Library/Application Support/iPhone Simulator/User/Applications. Find your application among the GUIDs, then just copy the sqlite store out into your List app's project folder.

Then, load that store like they do in the CoreDataBooks example.

like image 164
Ken Aspeslagh Avatar answered Oct 03 '22 23:10

Ken Aspeslagh


Yes there is in fact the CoreDataBooks example does this, you can download the code here: sample code

What you do is create the internal store (database) using the normal procedure to initialize your store just like you would with any other store, then you simply run your code and let it execute the code as described in the CoreDataBooks example (code snippet below). Once the store has been initialized you will want to create a NSManagedObjectContext and initialize it with the created persistent store, insert all the entities you need, and save the context.

Once the context has been successfully saved, you can stop your application, then go to finder and go to folder: ~/Library/Developer type in the search .sqlite and look under /Developer, sorting by date will give you the most recent .sqlite database which should match the time that the code was executed, you can then take this store and add it as a resource of your project. This file then can be read by a persistent store coordinator.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {  if (persistentStoreCoordinator) {     return persistentStoreCoordinator; }   NSString *storePath = [[self applicationDocumentsDirectory]      stringByAppendingPathComponent: @"CoreDataBooks.sqlite"];  /*   Set up the store.  For the sake of illustration, provide a pre-populated default store.  */ NSFileManager *fileManager = [NSFileManager defaultManager]; // If the expected store doesn't exist, copy the default store. if (![fileManager fileExistsAtPath:storePath]) {   NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"CoreDataBooks"      ofType:@"sqlite"];  if (defaultStorePath) {  [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];  } }  NSURL *storeUrl = [NSURL fileURLWithPath:storePath];   NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber   numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];   NSError *error;  if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {   // Update to handle the error appropriately.   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);  exit(-1);  // Fail }      return persistentStoreCoordinator; } 

Hope that helps.

-Oscar

like image 37
Oscar Gomez Avatar answered Oct 03 '22 22:10

Oscar Gomez