MagicalRecord:多个数据库

我有一个使用MagicalRecord的应用程序,并且预先使用大量用于参考的数据填充数据库。 在同一个数据模型中,我有用户可定义的信息,关于用户在应用程序中可能做的事情。

该应用程序被拒绝,因为预填充的数据应该被标记为“不备份”。 所以,我想在一个单独的数据存储中保存这些数据,这样我就可以保持用户数据不变。

有没有办法使用MagicalRecord有两个单独的数据存储?

我认为这是可能的,但不是太容易。 如您所知,要使用多个数据库,您应该对PersistentStoreCoordinator一些更改,以便拥有两个PersistentStores 。 在此之后,您的核心数据堆栈将如下所示: 在这里输入图像说明

另一种方式是两个独立的PersistentStoreCoordinators,每个携带一个商店。

在魔法logging中,有几个用于在NSPersistentStoreCoordinator + MagicalRecord.h中添加商店的类方法。

  • (NSPersistentStore *)MR_addInMemoryStore;
  • (NSPersistentStore *)MR_addAutoMigratingSqliteStoreNamed:(NSString *)storeFileName;
  • (NSPersistentStore *)MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__ autoreleasing NSDictionary *)options;

我想,这是你可以做你想做的事的地方。

另外我应该提到,设置堆栈的整个过程在MagicalRecord + Setup.h中

 + (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName 

所以你可以在那里添加你的店铺和协调员。 我从来没有自己pipe理,这只是一个可能的解决scheme的简要调查。

我能够使用configuration来解决这个问题。 由于魔法logging始终为configuration参数发送null ,因此我拆分了setupCoreDataStackWithAutoMigratingSqliteStoreNamed ,并将其replace为支持多个configuration的方法。

由于Magical Record在处理自动迁移方面做得不错,我首先调用setupCoreDataStackWithAutoMigratingSqliteStoreNamed ,然后进行清理,然后提供我的replace代码。

我有一个对象模型,我的种子数据对象分配了“种子”configuration和分配给“用户”configuration的用户对象。 魔法logging已经初始化,所以可以自动迁移,如果有必要的话。

 +(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]; } 

此外,MR 3.0具有并发堆栈,一旦完成就可以解决问题。

将不同的Core Data实体的数据保存在不同的存储文件中得到很好的支持,而且非常简单。 但是,MagicalRecrd没有提供任何方便的方法来设置你的Core Data栈。 您只需手动分配堆栈,并告诉MagicalRecord使用您创build的NSPersistentStoreCoordinator 。 以下是我如何迅速做到这一点:

 import Foundation import CoreData import MagicalRecord class CoreDataSetup { static func setupAutoMigratingStack(withContentConfigurationName contentConfigurationName: String, userConfirgurationNameName: String) { MagicalRecord.cleanUp() let managedObjectModel = NSManagedObjectModel.MR_defaultManagedObjectModel() let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel!) let contentURL = NSPersistentStore.MR_urlForStoreName(contentConfigurationName + ".sqlite") let userURL = NSPersistentStore.MR_urlForStoreName(userConfirgurationNameName + ".sqlite") let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption: true, NSSQLitePragmasOption: ["journal_mode": "DELETE"] ] do { try persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: contentConfigurationName, URL: contentURL, options: options) try persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: userConfirgurationNameName, URL: userURL, options: options) NSPersistentStoreCoordinator.MR_setDefaultStoreCoordinator(persistentStoreCoordinator) NSManagedObjectContext.MR_initializeDefaultContextWithCoordinator(persistentStoreCoordinator) } catch { print("Error adding persistent store to coordinator: \(error) ") } } } 

请注意,在我的代码中,我将“种子”存储的概念称为“内容”,将用户可定义的存储称为“用户”。

要完成问题的第二个方面,configuration内容存储库不被备份,您只需要使用存储每个存储的URL,将内容存储放置在未备份的临时目录中,然后复制它如果不存在,则从您的应用程序包启动到该位置。