8

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.

4 Answers 4

12

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.

3
  • @"BradLarson" It seems that this feature ("dragging the Core Data Entity item into the view") is discontinued in XCode4.
    – Ali
    Jul 2, 2011 at 15:09
  • What happens when you need to migrate the database on a future release? Would you would essentially have to rebuild a default database for every version of the model? I'm curious if it's maybe better to populate the database on first run from some property lists and I'd love to get your advice on it. Thanks Brad!
    – iwasrobbed
    Mar 18, 2013 at 19:56
  • 1
    @iWasRobbed - In that case, you'd migrate your bundled database to the latest version, as well. You'd want to avoid wasting time upgrading that for every iOS installation. My Mac clients that I've built for this have been able to update the databases to the latest schema for deployment in the iOS app bundles of newer application versions.
    – Brad Larson
    Mar 19, 2013 at 2:51
9

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.

1

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.

0

I would write a tool that populates a database with the data you want in it, generate the db at build time and stuff it in your resources folder. If you are not going to write to it you can just leave it there and access it directly, if you ever did want to write to it you would need to copy it somewhere you are allowed to write (like the documents folder).

1
  • This is a pretty old answer. Is this still true? If I'm not going to write to the prepopulated database, can I just read it directly from resources?
    – Suragch
    Aug 17, 2015 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.