简体   繁体   English

使用2个CoreData对象

[英]Working with 2 CoreData objects

I need to have 2 separate data bases on my project, so my question is, how can I have more than 1 CoreData data base on the same project? 我需要在我的项目上有2个独立的数据库,所以我的问题是,在同一个项目上我如何能拥有1个以上的CoreData数据库? Im asking this since I already have one coredata data base setup. 我问这个问题,因为我已经有一个coredata数据库设置。 I would be very thankful if I could have an explanation or a tutorial/example on how to address this problem. 如果能就如何解决此问题提供解释或教程/示例,我将非常感谢。

To create multiple Core Data stores, you make a separate xcdatamodel schema for each of the data stores. 要创建多个核心数据存储,请为每个数据存储创建一个单独的xcdatamodel模式。 In your application, you need to choose how you separate Core Data "stacks" for each of these schemata: 在您的应用程序中,您需要选择如何为以下每个模式分离核心数据“堆栈”:

  • you could have separate NSManagedObjectModel instances, one for each schema; 您可以有单独的NSManagedObjectModel实例,每个实例一个。 or you could create a merged model that contains the objects from both schemata. 或者您可以创建一个包含两个架构中的对象的合并模型。
  • you'll need at least one NSPersistentStore and NSPersistentStoreCoordinator for each managed object model - for most practical uses this means either one per model or a single store if you're using a merged model. 每个托管对象模型至少需要一个NSPersistentStoreNSPersistentStoreCoordinator对于大多数实际用途,这意味着每个模型一个或单个商店(如果您使用的是合并模型)。
  • you then create NSManagedObjectContext instances as you need them, backed by the persistent store coordinator(s). 然后,您可以根据需要创建NSManagedObjectContext实例,并由持久性存储协调器支持。

However, this is iOS, so users of your application will probably never manage or care about how your data stores are organised. 但是,这是iOS,因此应用程序的用户可能永远不会管理或关心数据存储的组织方式。 Unless you have a technical reason not to, you will find things easier just to define all of your entities in a single Core Data model. 除非出于技术原因,否则仅在一个Core Data模型中定义所有实体,您会发现事情变得简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM