MagicalRecord: multiple databases

前端 未结 3 1914
醉梦人生
醉梦人生 2021-02-08 11:36

I have an app that uses MagicalRecord, and I\'m pre-populating the database with a large amount of data that is used for reference. Within that same data model, I have user-defi

3条回答
  •  一向
    一向 (楼主)
    2021-02-08 12:15

    I was able to solve this issue using configurations. Since Magical Record always sends null for the configuration parameter, I broke apart setupCoreDataStackWithAutoMigratingSqliteStoreNamed and replaced it with a method that supports multiple configurations.

    Because Magical Record does a good job of handling auto migrations, I first call setupCoreDataStackWithAutoMigratingSqliteStoreNamed, followed by cleanup, and then I supply my replacement code.

    I have one object model with my seed data objects assigned the the "Seed" configuration and user objects assigned to the "User" configuration. Magical Record has already been initialized so it could auto migrate if necessary.

    +(void) RB_setupMultipleStores:(NSString *) seedStoreName userStore:(NSString *) userStoreName
    /* change persistent store to one with multiple configurations. Assumes Magical Record is initialized. */
    {
    NSError * error= nil;
    
    [MagicalRecord cleanUp];
    
    NSManagedObjectModel * model = [NSManagedObjectModel MR_defaultManagedObjectModel];
    
    NSURL *seedURL = [NSPersistentStore MR_urlForStoreName:[seedStoreName stringByAppendingString:@".sqlite"]];
    
    NSPersistentStoreCoordinator * coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             nil];
    
    NSPersistentStore * seedStore =[coordinator
                                      addPersistentStoreWithType:NSSQLiteStoreType
                                      configuration:@"Seed"
                                      URL:seedURL
                                      options:options
                                      error:&error];
    if (!seedStore || error)
    {
        NSLog(@"Error setting up seed store:%@ for %@", [error localizedDescription], seedURL);
        exit(-1);
    }
    
    NSURL *userURL = [NSPersistentStore MR_urlForStoreName:[userStoreName stringByAppendingString:@".sqlite"]];
    
    NSPersistentStore * userStore = [coordinator
                                     addPersistentStoreWithType:NSSQLiteStoreType
                                     configuration:@"User"
                                     URL:userURL
                                     options:options
                                     error:&error];
    
    if (!userStore || error)
    {
        NSLog(@"Error setting up user store:%@ for %@", [error localizedDescription], userURL);
        exit (-1);
    }
    [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
    
    [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
    }
    

    Also, MR 3.0 has concurrent stacks which may solve the problem once it is done.

提交回复
热议问题