Core Data Versioning manually

后端 未结 2 549
温柔的废话
温柔的废话 2021-02-06 05:00

I developed an app, iCollege, and now I want to make the app much better.
While the testing I wanted to restore the data from a backup. At launching iCollege the app crashes

2条回答
  •  醉梦人生
    2021-02-06 05:05

    You can do this as follows. Core data allows you adding multiple persistent stores to a NSPersistentStoreCoordinator, so you need two stores associated to your coordinator. Note that it is not possible to use two different Managed Object Models (MOMs), you can only have one MOM for all of your stores.

    While this may appear to be rather difficult, it is instead simple enough, given that Core Data also allows using configurations. A configuration has a name and an associated set of entities. The sets may overlap—that is, a given entity may appear in more than one configuration. See the associated documentation

    You can now easily deal with your problem by having different entities in different stores, creating a model which will be a superset of all the entities you're going to deal with (or at least the union) and that defines the subsets you need as configurations. Then, for each individual store, you specify the associated configuration.

    Something like (just an example snippet):

    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] init];
    [coordinator addPersistentStoreWithType:type configuration:@"CourseAndTeacher" URL:aURL options:nil error:NULL];
    [coordinator addPersistentStoreWithType:type configuration:@"SubjectAndProfessor" URL:anotherURL options:nil error:NULL];
    
    NSManagedObjectContext *context = [[NSManageObjectContext alloc] init];
    [context setPersistentStoreCoordinator:coordinator];
    

提交回复
热议问题