views:

327

answers:

4

Hi there, i am doing some experiments with coredata and objective-c. My application works fine in the simulator.

It also works on my iPodTouch. But how to i copy my local database to my device?

thanks,

+1  A: 

Check the question titled: How do I use my existing SQLite database with Core Data? in the Core Data FAQ.

You might also find this blog post helpful.

ahmadabdolkader
I think my questions wasn't clear enough. I'd like to use my existing SQLLite database (already working on the simulator) on my real device.//at the moment my app on the device shows an empty database :-(
fabian
+2  A: 

Copy the sqlite file out of the simulator directory structure (under ~/Library/Application Support/iPhone Simulator) and put it into your project to be included in the Resources directory. Once there you can either:

  1. Copy the file during runtime to your documents directory to make it writeable; or
  2. Reference it directly from within the app bundle to make it a read-only database

Depending on your application needs.

Marcus S. Zarra
Sounds good. But how to reference it directly from within the app bundle to make it a read-only database?
fabian
fabian
[[NSBundle mainBundle] pathForResource:@"Locations" ofType:@"sqlite"] will give you a string path back. Then turn that into a URL with [NSURL fileURLWithPath:] will return the url you then pass into Core Data.
Marcus S. Zarra
+1  A: 

This is the solution (Thanks Marcus S. Zarra)

Change this

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Locations.sqlite"]]

into this:

NSURL *storeUrl = [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"Locations.sqlite"]];
fabian