我收到警告“restkit.core_data:RKManagedObjectMappingOperationDataSource.m:264 Managed object cache returned 2 objects for the identifier configured for the ‘MyEntity’ entity, expected 1.”仅在我的设备上,但从未在模拟器中。
模拟器中的 sqlite 数据库很好,但在设备上我有相同的重复条目。如果我删除数据库,一段时间后警告再次弹出。但仅在设备上。
我正在使用 Restkit 0.23.1 通过删除孤立对象将 JSON 数据导入核心数据。设置了标识属性。
任何想法,如果模拟器和可能导致此问题的设备之间存在差异?
感谢您的帮助。
编辑
我收到各种实体的错误。这是我对实体 ResellerType
的映射(每个实体只有一个映射):
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"ResellerType"
inManagedObjectStore:[YPIDataProvider sharedDataProvider].objectManager.managedObjectStore];
[mapping addAttributeMappingsFromArray:@[ @"resellerTypeId",
@"resellerTypeName",
@"position" ]];
mapping.identificationAttributes = @[ @"resellerTypeId" ];
我与 Reseller
表有对多
关系,与我的元数据
有多对多
关系表。
这是我的 RestKit 设置:
NSError *error = nil;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:kDataModelName withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[self.managedObjectStore createPersistentStoreCoordinator];
[self.managedObjectStore
addSQLitePersistentStoreAtPath:self.pathToDatabase
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:@{ NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES }
error:&error];
[self.managedObjectStore createManagedObjectContexts];
// managedObjectCache: Configure a managed object cache to ensure we do not create duplicate objects:
NSManagedObjectContext *moc = self.managedObjectStore.persistentStoreManagedObjectContext;
self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc]
initWithManagedObjectContext:moc];
我在向服务器发送请求之前删除了 NSURLCache ([[NSURLCache sharedURLCache] removeAllCachedResponses]
)。你能发现这里有什么问题吗?
感谢阅读这一切!
最佳答案
我在 RestKit 问题 #1613 中找到了这个问题的答案:https://github.com/RestKit/RestKit/issues/1613 如果发生内存警告,RestKit 会刷新缓存。如果正在进行映射操作,则之后可能会有重复数据。
我在 RKEntityByAttributeCache.m 的 didReceiveMemoryWarning 中删除了 [self flush:nil];
,问题消失了。
关于ios - Restkit 创建重复项(错误 : "Managed object cache returned 2 objects") only on device, 不在模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24953011/