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>
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.
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.
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.
The persistent store should be located in the AppData > Library > Application Support directory.
Two options spring to mind:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With