Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default dataset for Core Data based iPhone application

I am writing an iPhone 3.0 application that uses Core Data to persist the model. I'd like the application to be installed with a default dataset. When developing for iPhone < 3.0 I used an SQL script to initialize a database prior to running a build and then deployed the prepared .sqlite file as an application resource. What's the best approach with Core Data.

A conclusion: In the end I wrote a generic XML handler. Element names are mapped to Objective-C class names and property names. PCDATA values within elements were converted into the type declared on the property named by the element. Child elements or property elements were resolved to object instances - and thus through parsing an XML document an object graph was built. I had to get to grips with the Objective-C runtime first though :-)

Example target classes:

@interface Widget : NSObject {
@private
    NSString* name;
    NSSet* sprockets;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSSet* sprockets;
- (void)addSprocketsObject:(Sprocket*)value;    
@end

@interface Sprocket : NSObject {
@private
    NSString* name;
    NSNumber* canFly;
    NSNumber* wheels;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSNumber* canFly;
@property (nonatomic, retain) NSNumber* wheels;
@end

Example default data:

<data>
    <Sprocket id="sprocket-1">
        <name>Sprocket1</name>
        <wheels>4</wheels>
    </Sprocket>
    <Widget id="widget-1">
        <name>MyWidget</name>
        <sprockets>
            <Sprocket ref-id="sprocket-1"/>
            <Sprocket id="sprocket-2">
                <name>Sprocket2</name>
                <canFly/>
            </Sprocket>
            <Sprocket id="sprocket-3">
                <name>Sprocket3</name>
            </Sprocket>
        </sprockets>
    </Widget>
</data>
like image 692
teabot Avatar asked Jun 10 '09 21:06

teabot


People also ask

What is Core Data on iPhone?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

What database does Core Data use?

Even though Core Data knows how to use a SQLite database as its persistent store, that doesn't mean you can hand it any SQLite database. The database schema of the SQLite database used by Core Data is an implementation detail of the framework. It isn't publicly documented and liable to change.

What is Core Data in iOS development?

Overview. Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

Where does Core Data store Data?

The persistent store should be located in the AppData > Library > Application Support directory.


2 Answers

Two options spring to mind:

  1. Write an importer from some reasonable data format (XML, JSON, etc.) and import that into your Core Data context on first run, then save the context to a persistent store.
  2. If your app only needs one persistent store, you can pre-populate it and deploy the persistent store with your app's resources. If you need multiple persistent stores, all pre-populated with the same default data, option 1 is probably going to be easier, but you could use NSPersistenStoreCoordinator's migratePersistentStore:toURL:options:withType:error: (or the equivalent in iPhone Core Data -- still under NDA) to create the new store from the pre-poplated store for each new store needed.

In my experience the code to implement option 1 is nearly the same code as required to prepopulate a persistent store, so perhaps there's really only one option with two points of view.

like image 63
Barry Wark Avatar answered Oct 06 '22 02:10

Barry Wark


You can use Plist to store the initial data and populate your persistent store on first run. This approach is easier than having to write your own custom XML parser.

like image 23
Boon Avatar answered Oct 06 '22 03:10

Boon