【发布时间】:2013-06-28 02:40:19
【问题描述】:
我的项目设计要求我使用两个不同的RKObjectManagers 和两个不同的baseURLs 创建两个完全独立的持久存储:
manager 1 : 将处理 store1.sqlite
经理2:将处理store2.sqlite
所以,据我所知,我必须创造:
1) 2 个独立的数据模型:模型 1 和模型 2
2) 2 个独立的托管对象上下文:上下文 1 和上下文 2
3) 1 persistentStoreCoordinator
我之前曾经设置RestKit 0.2x 来处理核心数据(1 个商店),如下所示:
NSError *error = nil;
NSURL *modelURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"store1" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel1 = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL1] mutableCopy];
RKManagedObjectStore *managedObjectStore1 = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel1];
[managedObjectStore1 createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store1.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (error) {
NSLog(@"unresolved error %@, %@", error, [error userInfo]);
abort();
}
[managedObjectStore1 createManagedObjectContexts];
manager1.managedObjectStore = managedObjectStore1 ;
managedObjectStore1.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore1.mainQueueManagedObjectContext];
manager1 是RKObjectManager
我的问题是:
1) 我应该对这段代码进行哪些更改才能正确设置 2 个完全独立的持久性存储?
2)我如何访问与(例如store1)相关的所需上下文,我通常使用managedObjectStore1.mainQueueManagedObjectContext]?
提前谢谢你。
附言在applicationDidFinishLaunchingWithOptions 我必须通过简单地删除带有NSFileManager 的sqlite 文件并创建一个新的干净文件来擦除所有store 2 数据,有人可能会问为什么还要保留数据?因为使用内存中的 NSObjects 会杀死 iPhone 内存,在我的情况下,有数千条记录可供使用。
【问题讨论】:
-
为什么需要两个完全独立的 RestKit/CoreData 堆栈?只有两个持久存储还不够吗?
标签: iphone objective-c cocoa-touch restkit-0.20