5

I'm trying create 2 apps, one that builds a persistent store, and another one that consumes it.

So far I have built one app that uses CoreData to successfully build a database from an XML file. So this project contains the data model, the .h/.m files for the entities, etc.

I'm now trying to enable the second app to read that .sqlite file by copying the data model file, the .h/.m files related to the entities and the sqlite file to that project (via add existing).

The code executes but always fails to find any objects in the database.

Are there any restrictions or correct steps to take when trying to copy over these files?

rhess
  • 93
  • 1
  • 5

5 Answers5

4

The solution here is deceptively simple.

Just copy your .xcdatamodel file from one project to another and then when you run your app in the simulator for the first time it'll create a Documents folder for the app. Just drop your saved .sqlite or .binary files into the yourApp/Documents directory on the device. You can find the simulator directory in "~user/Library/Application Support/iPhone Simulator".

You can also download, edit, and upload the myApp directory on a provisioned iPhone by dragging and dropping into and out of the Organizer. Look at the Applications list.

The iPhone doesn't support xml stores with core data, only sqlite or atomic (binary). The sqlite store is by far the better option for most applications since it doesn't all have to be loaded into memory at runtime.

Is this what you meant?

mjmdavis
  • 2,583
  • 3
  • 19
  • 16
  • Thanks! I copied the .xcdatamodeld package from one project to another (with a different name). I even renamed the file & the `.xcdatamodel` file within it (show package contents) to match the name of the other project, and it still seems to work OK. Kinda scary cause it's all binary to me. I wish there were some way to diff the files. Hopefully, the files don't contain any project-specific things (like project name). Make sure your code that sets up the Core Data stack uses the new name. And, you may want to delete the app before running it again so that it can recreate the SQLlite file. – ma11hew28 Apr 29 '11 at 16:12
0

I think (not 100 percent sure) in your app plist, if you set your application bundle name to the same thing, they will share resources because the device will think they are the same application...

Daniel
  • 22,363
  • 9
  • 64
  • 71
0

I don't think this is going to work the way that you want it to. On the iPhone, each application runs in its own "sandbox", and it's not really possible for one application to write files that another can read.

Mark Bessey
  • 19,598
  • 4
  • 47
  • 69
  • 1
    But i believe if they have the same bundle path, they will share resources – Daniel Jul 13 '09 at 15:51
  • 1
    But they are considered the same application – Daniel Jul 13 '09 at 15:52
  • Actually, I should have been more clear in my original post. What I'm trying to do is run one app in simulator to create a database (persistent store) out of a whole lot of XML files. Then, copy over data model + database and entity related .m/.h files, to the actual application that will mostly just read that database. So once on the actual device, I'm just working within the second app. Does that make sense? Thanks for the help! – rhess Jul 13 '09 at 17:59
0

Is it really two different projects, or is it two targets in the same project? That would seem to make a lot more sense, and then you can share entity objects as they change.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • Currently I really have them as 2 different projects. one is the 'Data Store Creator' that loads entities from XML into a persistent SQLite store, the other the actual app. – rhess Jul 13 '09 at 19:07
0

For copying core data files from one project to another, I first created a new project with core data support and then I opened the contents of the previous coredata file and except the root tag, pasted all child tags in the new core data file in new project. Previousy I tried to delete the coredata file in new project, copy pasted the previous one and changed its name and it was not working.

muzz
  • 4,324
  • 2
  • 24
  • 14