Tim*_*van 10 core-data ios magicalrecord
我有一个使用MagicalRecord的应用程序,我使用大量数据预先填充数据库,用于参考.在同一个数据模型中,我有用户可定义的信息,这些信息与用户在应用程序中可能执行的操作有关.
应用程序被拒绝,因为预先填充的数据应该标记为"不备份".所以,我想将这些数据放在一个单独的数据存储区中,这样我就可以将用户数据保持为可备份数据.
有没有办法使用MagicalRecord拥有两个独立的数据存储区?
Nik*_*rov 15
我认为这是可能的,但不是太容易.如您所知,要使用多个数据库,您应该对您进行一些更改PersistentStoreCoordinator,因此它将有两个PersistentStores.在此之后,您的Core Data堆栈将如下所示:

另一种方法是两个制作两个独立的PersistentStoreCoordinator,每个都携带一个商店.
在Magical Record中,有几种类方法可以在NSPersistentStoreCoordinator + MagicalRecord.h中添加存储 .
- (NSPersistentStore*)MR_addInMemoryStore;
- (NSPersistentStore*)MR_addAutoMigratingSqliteStoreNamed:(NSString*)storeFileName;
- (NSPersistentStore*)MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__ autoreleasing NSDictionary*)选项;
我想,这是你可以做你想做的事情的地方.
另外我应该提一下,设置堆栈的整个过程都在MagicalRecord + Setup.h中
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
Run Code Online (Sandbox Code Playgroud)
所以你可以在那里添加你的商店和协调员.我自己从未管理过,这只是对可能解决方案的简要调查.
我能够使用配置解决这个问题.由于魔法记录总是发送null配置参数,我分开setupCoreDataStackWithAutoMigratingSqliteStoreNamed并用支持多种配置的方法替换它.
因为Magical Record可以很好地处理自动迁移,所以我先打电话setupCoreDataStackWithAutoMigratingSqliteStoreNamed,然后清理,然后我提供替换代码.
我有一个对象模型,我的种子数据对象分配了"种子"配置和分配给"用户"配置的用户对象.Magical Record已经初始化,因此可以根据需要自动迁移.
+(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];
}
Run Code Online (Sandbox Code Playgroud)
此外,MR 3.0具有并发堆栈,一旦完成就可以解决问题.
| 归档时间: |
|
| 查看次数: |
3641 次 |
| 最近记录: |