简体   繁体   English

具有单个持久性存储协调器的多个数据模型

[英]Multiple data models with single persistent store coordinator

I inherited an iOS project that uses Core Data. 我继承了一个使用Core Data的iOS项目。 This project has 8 different data models , no need to say that the project is not that big and that I can not see any good reason for splitting the entities over so many data models. 这个项目有8个不同的数据模型 ,不必说项目不那么大,而且我看不出有任何充分的理由将实体拆分成这么多的数据模型。

I am trying to use Encrypted Core Data with the current data models and persistent store coordinators and it is not working at all. 我正在尝试将Encrypted Core Data与当前数据模型和持久性存储协调器一起使用,但它根本无法正常工作。 Every data model is initialized like this: 每个数据模型都是这样初始化的:

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"First"
                                              withExtension:@"momd"];

    self.model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    // Coordinator
    //NSPersistentStoreCoordinator *psc = [EncryptedStore makeStore: self.model passcode: @"pass"];
    [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];

    NSURL *storeURL = [[[AppDelegate appDelegate] applicationDocumentsDirectory] URLByAppendingPathComponent: @"First.sqlite"];

    NSError *error = nil;
    [psc addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil
                                        URL:storeURL
                                    options:nil
                                      error:&error];

    NSManagedObjectContextConcurrencyType ccType = NSMainQueueConcurrencyType;

    self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:ccType];
    [self.context setPersistentStoreCoordinator:psc];

So every data model has its own managed object model and its own persistent store coordinator with its persistent store and context. 因此,每个数据模型都有自己的托管对象模型以及带有持久性存储和上下文的持久性存储协调器。

What I see is that Encrypted Core Data (ECD) is only creating the tables in the firstly created persistent store. 我看到的是,加密核心数据(ECD)仅在首先创建的持久性存储中创建表。 My suspicion is that ECD only handles the persistent stores added to a single coordinator. 我的怀疑是,ECD仅处理添加到单个协调器中的持久性存储。 Based on that assumption I am wondering if it is possible to create a single coordinator and add several stores to it. 基于这个假设,我想知道是否有可能创建单个协调器并向其中添加多个存储。

I am not that familiar with Core Data but I can't see how that would be possible since the coordinator is initialized with the managed object model (that points to a specific data model file containing only a set of the total number of entities in the project). 我对Core Data不太熟悉,但是由于协调器是使用托管对象模型初始化的,所以我看不到这是怎么可能的(指向特定数据模型文件的文件中仅包含一组实体总数)项目)。

Any ideas? 有任何想法吗? I really would like to avoid merging all the data models into a single one in order to use a single managed object model and coordinator (Actually I would like to do it but I am sure it would break everything and I don't really have to time for that right now). 我真的很想避免将所有数据模型合并到一个模型中,以便使用单个托管对象模型和协调器(实际上,我想这样做,但是我敢肯定,它将破坏一切,而且我并不需要现在的时间)。

You can't use a single persistent store coordinator without merging the models. 如果不合并模型,则不能使用单个持久性存储协调器。 However, you don't have to edit your data models-- you can merge them at run time. 但是,您不必编辑数据模型-您可以在运行时合并它们。 NSManagedObjectModel offers a couple of different ways to merge multiple models into a single unified model. NSManagedObjectModel提供了几种不同的方式将多个模型合并为一个统一的模型。 If you load each model independently and merge them in code, you get a single NSManagedObjectModel representing the combined model from each model file. 如果独立加载每个模型并将其合并到代码中,则会从每个模型文件中获得一个表示组合模型的NSManagedObjectModel You could then use that combined model with a single persistent store coordinator. 然后,您可以将组合模型与单个持久性存储协调器一起使用。

If you're still using multiple model files, you can add each one separately. 如果您仍在使用多个模型文件,则可以分别添加每个模型文件。 This raises a complication though-- how will Core Data know which model file to use when you create a new model object instance? 但是,这带来了一个复杂的问题-创建新的模型对象实例时,Core Data将如何知道要使用哪个模型文件? You would have to use the assignObject:toPersistentStore: method on NSManagedObjectContext to tell it which one to use. 您必须在NSManagedObjectContext上使用assignObject:toPersistentStore:方法来告诉它要使用哪一个。 Every time you create a new instance, you do this as well. 每次创建一个新实例时,您也会这样做。 This means that you need to keep references to the NSPersistentStore instances for each file, and know which to use in every case. 这意味着您需要为每个文件保留对NSPersistentStore实例的引用,并知道在每种情况下NSPersistentStore使用哪个实例。

I should add that I have not used encrypted Core Data so I don't know if this will solve your real problem. 我应该补充一点,我没有使用加密的核心数据,所以我不知道这是否可以解决您的实际问题。 This approach will allow multiple model files and multiple persistent stores with a single coordinator, though. 但是,这种方法将允许使用一个协调器来多个模型文件和多个持久性存储。

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

相关问题 多个持久性存储协调器核心数据 - Multiple Persistent Store Coordinator Core Data iOS中的多个持久性存储协调器 - Multiple persistent store coordinator in iOS CoreData多个持久性存储协调器? - CoreData multiple persistent store coordinator? 核心数据持久性存储协调器不存在 - Core data persistent store coordinator isnt present 核心数据:无法将持久性存储添加到协调器 - Core Data: Not able to add persistent store to coordinator 重置存储时核心数据崩溃:'无法从此NSManagedObjectContext的协调器访问对象的持久存储' - Core data crash when resetting the store: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator' 核心数据堆栈:此Apple提供的示例将Persistent Store Coordinator异步添加到堆栈中。 为什么? - Core Data Stack: This Apple provided example adds the Persistent Store Coordinator to the stack asynchronously. Why? Coredata:如何使用一个持久存储协调器交替使用 2 个持久存储? - Coredata: How to use alternatively 2 persistent stores with one persistent store coordinator? 托管对象存储无法创建持久存储协调器 - Managed object store failed to create persistent store coordinator 无法从此NSManagedObjectContext的协调器访问对象的持久性存储 - Object's persistent store is not reachable from this NSManagedObjectContext's coordinator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM