How to save data when using one ManagedObjectContext and PersistentStoreCoordinator with two stores

STATEMENT OF THE PROBLEM

When I try to save a record in the read / write repository, which is one of two SQLite repositories assigned to the same PersistentStoreCoordinator, my iPhone application crashes. One of the obvious problems when saving a record - PersistentStoreCoordinator does not know in which store to save data (just because I do not know how to do it).

First I will provide a big picture to make sure my approach sounds. Then I will provide implementation details.

BACKGROUND

This is a simplified example that introduces key aspects of the real application I'm working on.

Managed Object Model

Seed data

Seed data

User input script

User Input Scenario

CURRENT IMPLEMENTATION

Implementation of Master Data

Core data implementation

Data storage and retrieval

, , , , .

- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
   if (_persistentStoreCoordinator == nil) {
       NSArray *bundles = @[[NSBundle bundleForClass:[self class]]];
       _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:bundles]];

       NSError *error;
       //--------------------------------------------------
       // Set options for the USER DATA Persistent Store.
       NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                       NSInferMappingModelAutomaticallyOption : @YES};
       //--------------------------------------------------
       // Add the USER DATA Store to the Persistent Store Coordinator.
       NSPersistentStore *persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                                   configuration:nil
                                                                                             URL:self.persistentStorePathForUserData
                                                                                         options:options
                                                                                           error:&error];
       //--------------------------------------------------
       // Set options for the SEED DATA Persistent Store.
       options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                         NSInferMappingModelAutomaticallyOption : @YES,
                                NSReadOnlyPersistentStoreOption : @YES};
       //--------------------------------------------------
       // Add the SEED DATA Store to the Persistent Store Coordinator.
       persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                configuration:nil
                                                                          URL:self.persistentStorePathForSeedData
                                                                      options:options
                                                                        error:&error];
   }
   return _persistentStoreCoordinator;
}

:

  • , , ( Seed Data, - ) , n n + 5.
  • ManagedObjectModel. , .

RESEARCH

, , , , . , , . (. № 2 .)

, iOS Core?, , - /. Seed , - /. , . (. № 2 .)

Apple, " ", 4 " " , , . .

, SQLite , , , , .

, Core Data (2- ), Marcus Zarra, , . 3 . ( № 1 .)

, iPhone, ManagedObjectModel, . , , .

, NSPersistentStoreCoordinator ?, , . , . , NSFetchRequest, setAffectedStores, NSManagedObjectContext .

+2
2

Ray Wenderlich, Mic Pringle, Mic , , . .

Managed Object Model

, : 1) UserData - r/w, .

UserData Configuration

2) SeedData - r , App Bundle.

SeedData Configuration

, ( ), , , , .

+2

/ , :

  • addPersistentStore NSPersistentStore.
  • :

    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Vehicle" inManagedObjectContext:self.managedObjectContext];
    [self.managedObjectContext assignObject:newObject toPersistentStore:userEditableStore];
    

    , assignObject:toPersistentStore: .

:

, , ...

(.. ), . , .

, , ( ), . .

ManagedObjectModel. , , .

, , Core Data. ( , ), . , . , .

0

Source: https://habr.com/ru/post/1536701/


All Articles