iOS RestKit不能将本地实体保存到数据库 [英] iOS RestKit can not save local entity to database
问题描述
我使用RestKit 0.20来解析JSON数据并保存到数据库。
这是一个映射实体SchoolClass,由RestKit处理并保存。
我有另一个实体叫MyClass,它存储我选择的类。这一个只在设备上本地。
I am using RestKit 0.20 to parse JSON data and save to database. THere is a mapped entity SchoolClass, which is handled by RestKit and saves fine. I have another entity called MyClass, which stores the classes I have selected. This one is only local on the device.
下面是我创建并保存MyClass实体的代码
Here is the code I create and save the MyClass entity
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
MyClass* course = [managedObjCtx insertNewObjectForEntityForName:@"MyClass"];
.. set the data for course here
NSError *executeError = nil;
if(![managedObjCtx save:&executeError]) {
NSLog(@"Failed to save to data store");
}
以下是初始化托管数据存储的代码
Here is the code that initialize the managed data store
// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
/**
Complete Core Data stack initialization
*/
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKMainDb.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
看起来保存成功,在MyClasseTableViewController中,我可以读取保存的MyClass条目。但是,在我关闭应用程序,然后重新重新启动。 MyClassTableViewController为空,因为获取的结果为空。我使用SQLiteBrowser打开sqlite文件,MyClass表为空。看起来MyClass实体只保存在缓存中,但不在持久存储中。我需要调用RestKit提供的一些API来保存它吗?我试图通过文档阅读,但找不到它。请帮助。
It appears the save is successful and in the MyClasseTableViewController I could read the saved MyClass entries. However after I close the app and restart again. The MyClassTableViewController is empty, because the fetched results is empty. I opened the sqlite file using SQLiteBrowser and the MyClass table is empty. Looks like the MyClass entities are only saved in the cache but not in the persistent store. Do I need to call some API provided by RestKit to save it? I tried to read through the doc but could not find it. Please help.
推荐答案
感谢Tom的领导,我发现RestKit有NSManagedObjectContext(RKAdditions),它有一个方法:
Thanks for the lead by Tom, I found that RestKit has NSManagedObjectContext (RKAdditions), which has a method:
- (BOOL)saveToPersistentStore:(NSError **)error
是的,它有处理嵌套的受管对象上下文的逻辑。
这是新的代码工作,只是一行更改,但花了很多时间来找到正确的调用:(
Yes it does have logic to handle nested managed object context. Here is the new code that works, just one line change, but took a lot of time to find the right call :(
#import "NSManagedObjectContext+RKAdditions.h"
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
MyClass* course = [managedObjCtx insertNewObjectForEntityForName:@"MyClass"];
.. set the data for course here
NSError *executeError = nil;
if(![managedObjCtx saveToPersistentStore:&executeError]) {
NSLog(@"Failed to save to data store");
}
这篇关于iOS RestKit不能将本地实体保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!