如何拥有多个应用程序 - 一个核心数据? [英] How to have multiple apps - one Core Data?

查看:123
本文介绍了如何拥有多个应用程序 - 一个核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的开发人员,但刚开始接触Mac。我只是不想下去一个路径,只是为了发现我做了一些基本的错误或不正确的假设后来。



我想最终构建和销售iPhone应用程序使用Core Data。该应用程序将免费提供内容通过应用程序内购买。这是我想要做的:



选项1


  1. 创建指向相同Core Data对象模型但具有自己的主数据库的Mac OS X实用程序应用程序。

  2. 使用Mac应用程式。

  3. 将Mac应用程式中的主要资料子集汇出为主要资料子集的平面档案(XML?)。

  4. 用户购买数据时,从云端下载并将数据导入本地iPhone数据存储。

第2应该很容易。我已经阅读关于XML解析器应该帮助我#4。我需要帮助#1和3。



对于#1,我不知道如何使用Xcode为两个应用程序维护一个对象模型。该数据模型必须接受模型版本控制。我只创建两个项目,一个Mac和一个iPhone,并将它们指向相同的.xcdatamodel文件,魔法发生在我身上?



对于#3,有没有人可以共享的示例代码将通过对象数组迭代来创建XML?



选项2 b
$ b

我正在考虑的另一个选择在下面讨论。



我可以维护一个独立的元数据数据库,该数据库包含有关以下信息的信息:



然后,我可以从本地文档目录动态访问单个SQL文件。这类似于iBooks模型,其中sql文件等同于单独的书。



我想每次只能有两个活动的数据库连接...一个用于元数据,另一种用于特定的书。我不知道是否这将扩展到许多(几十或几百)sql文件,但是。



任何帮助是赞赏!



Jon



UPDATE :我刚看到Marcus Zarra的回答:



删除和添加持久存储到核心数据应用程序



这似乎是选项2是一个坏主意。

解决方案

对于(1),您可以在两个应用程序中使用相同的对象模型。事实上,如果您使用相同的Core Data生成存储,则需要这样做。简单地说,在两个应用程序中包括相同的模型文件。在Xcode中,最简单的方法是将模型文件放在每个项目的项目文件夹外部,然后添加模型文件,而不将其复制到项目文件夹。这将确保两个应用程序对每个构建使用相同的模型文件。



对于(3),您需要首先使用与引用存储相同的模型创建一个导出永久存储,并将其添加到参考上下文。在模型中,创建导出配置。为模型中的每个实体创建子实体,但不更改任何属性或关系。将这些实体分配到导出配置。



您需要为引用实体的每个ManagedObject子类添加一个Clone方法。当触发时,该方法将返回一个由引用对象属性和关系填充的子实体(关系对象也将被克隆。)



请注意,克隆对象图是递归的,可以使用大量的内存。



保存时,由于您将它们分配到导出配置,所有克隆的导出实体及其关系将保存到导出存储。您将不仅克隆对象,而且克隆相关对象图。



在iPhone应用程式中加入模型和汇出商店。编写应用程序以仅使用导出实体。它永远不会注意到没有任何引用对象。



对于(4),我不会使用XML或在核心数据之外导出数据。我只使用在(3)中创建的导出Core Data SQL存储,并使用它。


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 an 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.

这篇关于如何拥有多个应用程序 - 一个核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆