Pergunta

I'm creating an app that will have a lot of information pre-stored within it.

Would it be best to use a plist to store the data, or Core Data with SQLite? What is the fastest method to pre-populate the data in the app?

To give you an idea of the type of data I'm going to be using, you can think of storing movie information. I'm not actually going to be storing movies, but the data I am storing is probably similar in structure: One movie can have multiple actors, one actor can play in multiple movies. Basically, there are many to many relationships in my data.

Foi útil?

Solução

If you want to be able the start with a complete database, the fastest way would be to create a persistent store file that already has all the data in it and add it to your apps bundle. When the app launches for the first time, you could copy the the file from the bundle to the documents directory before setting up your NSPersistentStoreCoordinator and start using it:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
if(![fileManager fileExistsAtPath:[storeURL path]]) {
  NSURL *template = [NSURL fileURLWithPath:[NSBundle pathForResource:@"start-database" ofType:@"sqlite" inDirectory:nil]];
  [fileManager copyItemAtURL: toURL:storeUrl error:nil];
}

After that continue as normal with the setup of your NSPersistentStoreCoordinator.

You could pre-populate the database by running the app yourself, adding all the items, saving the context, and then quitting the app. After that copy the created sqlite database file and add to the resources of your app.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top