Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize a store with default data in a CoreData application?

I'm doing an iPhone application. In this app, I just want to have a database to be used as a looked up table for values in my app. The only thing the database will do was to supply me the values I needed depending on the query of the program. It won't do any addition or deletion in the database. My question was how do I initialize a default data on the application using CoreData. So that when the program starts It already had all the values needed.

If you have other suggestions of what is better do or what are other alternatives, please tell me.

Thanks.

like image 608
edie Avatar asked Aug 12 '09 05:08

edie


3 Answers

I would create a simple Mac application, starting from the template for a Core Data document-based application. Copy your existing .xcdatamodel over the default one in the project (or add the new data model and be sure to rename anywhere that refers to the default model). Open up the XIB file for the document window in Interface Builder and drag the Core Data Entity item into it from the Interface Builder library inspector. From the resulting dialog, choose an entity to display and select an interface to display it in.

What this will do is create a fully functional interface for adding, removing, or editing the properties of that entity type. Everything should be hooked up via Cocoa Bindings so that you don't need to write a line of code for this to work. You can add interfaces for each entity type in your model this way.

This will let you quickly enter and edit data within a Core Data document, which you can then save out to disk and add as a resource to your iPhone application. The SQLite database structures are fully compatible between the desktop and iPhone Core Data implementations, so I've found that this is a quick and easy way of testing your iPhone Core Data code.

like image 91
Brad Larson Avatar answered Sep 21 '22 21:09

Brad Larson


Please refer to the Core Data Programming Guide, or see below (copy from the PG):

" How do I initialize a store with default data?

There are two issues here: creating the data, and ensuring the data is imported only once. There are several ways to create the data.

  • You can create a separate persistent store that contains the default data and include the store as an application resource. When you want to use it, you must either copy the whole store to a suitable location, or copy the objects from the defaults store to an existing store. For small datasets, you can create the managed objects directly in code.

  • You can create a property list—or some other file-based representation—of the data, and store it as an application resource. When you want to use it, you must open the file and parse the representation to create managed objects.

You should not use this technique on iOS, and only if absolutely necessary on Mac OS X. Parsing a file to create a store incurs unnecessary overhead. It is much better to create a Core Data store yourself offline and use it directly in your application. There are also several ways to ensure that the defaults are imported only once:

  • If you are using iOS or creating a non-document-based application for Mac OS X, you can add a check on application launch to determine whether a file exists at the location you specify for the application’s store. If it doesn't, you need to import the data. For an iOS-based example, see CoreDataBooks .

  • If you are creating a document-based application using NSPersistentDocument, you initialize the defaults in initWithType:error:.

  • If there is a possibility that the store (hence file) might be created but the data not imported, then you can add a metadata flag to the store. You can check the metadata (using metadataForPersistentStoreWithURL:error:) more efficiently than executing a fetch (and it does not require you to hard code any default data values).

"

As mentioned above, generally we need to create a pre-populated default store with code, then use it as a resource file, and copy it from resource bundle to document directory if the coredata file is missing. Please search the CoreDataBooks code example in your Xcode Organizer (or Apple Developer Center), and look at the - (NSPersistentStoreCoordinator *)persistentStoreCoordinator method.

like image 40
flypig Avatar answered Sep 19 '22 21:09

flypig


I racked my brain for hours attempting to solve this. What I came up with was simply not to save the database. That way, it will be initialized each time the app is opened. If you save it, it will continue to duplicate.

like image 26
Clifton Crawley Avatar answered Sep 19 '22 21:09

Clifton Crawley