开发者

How to have multiple apps - one Core Data?

I’m an experienced developer, but new to Mac. I just don’t want to go down one path only to find out that I made some fundamental error or incorrect assumption later on.

I want to ultimately build and sell a开发者_如何学Pythonn iPhone app using Core Data. The app will be free with content available through in-app purchase. Here is what I want to be able to do:

OPTION 1

  1. Build a Mac OS X utility app that points to the same Core Data object model, but has its own “master” database.
  2. Populate the master database using the Mac app.
  3. Export a subset of the master data from the Mac app to a flat file (XML?) that is a subset of the master data.
  4. When the user purchases that data, download from the cloud and import that data into the local iPhone data store.

Number 2 should be easy enough. I have read about the XML Parser that should help me with #4. I need help with #1 and 3.

For #1, I can’t figure out how I can maintain one object model for both apps with Xcode. That data model must accept model versioning. Do I just create two Projects, one Mac and one iPhone, and point them both to the same .xcdatamodel file and the magic happens for me?

For #3, is there any sample code that someone can share that will iterate through an array of objects to create the XML?

OPTION 2

Another option I am considering was discussed below. Instead of worrying about import/export, simply create individual sql files for each set of new or updated data.

I could maintain a separate "metadata" database that has information about the individual sql files that are available to the app.

Then, I can dynamically access the individual SQL files from the local documents directory. This is similar to an iBooks model where the sql files equate to individual books.

I'm thinking I could have only two active database connections at a time... one for the metadata and the other for the specific "book". I am not sure if this will scale to many (tens or hundreds) sql files, however.

Any help is appreciated!

Jon

UPDATE: I just saw the answer from Marcus Zarra at:

Removing and adding persistent stores to a core data application

It sounds like Option 2 is a bad idea.


For (1), you can use the same object model in both apps. Indeed, if you use the same Core Data generated store, you are required to do so. Simply, include the same model file in both apps. In Xcode, the easiest way to do this is to put the model file external to the project folders of each project and then add the model file without copying it to the project folder. This will ensure that both apps use the same model file for every build.

For (3), you need to first create an "export" persistent store using the same model as the reference store and add it to the reference context. In the model, create an "Export" configuration. Create a subentity for every entity in the model but do not change any attributes or relationships. Assign those entities to the Export configuration.

You will need to add a "Clone" method to each ManagedObject subclass for the reference entities. When triggered, the method will return a subentity populated with the reference objects attributes and relationships (the relationship objects will be cloned as well.)

Be aware that cloning an object graph is recursive and can use a lot of memory.

When you save, because you assigned them to the "Export" configuration, all the cloned export entities and their relationships will be saved to the export store. You will have cloned not only the objects but the related object graph.

Include the model and the export store in the iPhone app. Write the app to make use of the export entities only. It will never notice the absence of any reference objects.

For (4), I wouldn't mess around with using XML or exporting the data outside of core data at all. I would just use the export Core Data SQL store created in (3) and be done with it.


You can give a NSManagedObjectContext instance and instance of NSPersistentStoreCoordinator. This class has options allowing you to specify a file location for sotring data and a format (SQLite, Binary, or XML)


How do you plan to actually transfer data from Mac to iPhone? Is this something you do during development, or something people do during daily use? If the latter, you are probably better off building decoupled export/import into your app right away. So the Mac would serialize data into XML or JSON, push it somewhere in the cloud (not sure if local network/bonjour transfer is easier or useful, cloud is more universal), and iPhone fetches the data and deserializes it into the local schema/repository. You should not plan to work on the SQL layer with Core Data. Different platforms may use a different storage backend.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜