我正在迁移我的连接类以支持通过CoreData进行缓存。 RestKit使用RKEntityMapping自动完成。 RestKit和CoreData正确地进行了映射和验证,我得到了一堆我请求的NSManagedObject但它没有将对象保存到CoreData。当我检查日志时,我得到了这个。
W restkit.network.core_data:RKManagedObjectRequestOperation.m:776 Saving of managed object context failed, but a `nil` value for the `error` argument was returned. This typically indicates an invalid implementation of a key-value validation method exists within your model. This violation of the API contract may result in the save operation being mis-interpretted by callers that rely on the availability of the error.
E restkit.network.core_data:RKManagedObjectRequestOperation.m:818 Failed saving managed object context to the persistent store <NSManagedObjectContext: 0x145e5ce0>: (null)
我不知道,为什么会这样。我检查了我的模型是否有错误的设置或属性,但我无法找到。正如我所观察到的,NSManagedObjects被保存在InMemory中(即使我离开视图,NSFetchResultsController仍然能够获取对象。所以我猜,对象被保存在InMemory中)。有人可以帮我找到错在这里。留下任何调试问题的提示是值得欢迎的。谢谢。
我有这个代码来设置核心数据。
- (void)setupCoreData
{
NSManagedObjectModel *managedObjectModel = [[AppDelegate sharedDelegate] managedObjectModel];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[[RKObjectManager sharedManager] setManagedObjectStore:managedObjectStore];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Model.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} 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];
}
在添加所有响应/请求描述符之前调用此方法。我使用MoGenerator生成了模型类。我重写了一些setter并在插入(validateForInsert)之前做了一些验证。