Restkit +核心数据:在每个应用程序启动时在UITableView中插入重复值

时间:2013-07-11 06:54:53

标签: core-data restkit restkit-0.20

关注AlexEdge Tutorial我遇到了以下行为。我可以按要求提供代码,因为有很多行,我不确定在哪里查看。基本上,它在数据被正确加载到UITableView的意义上“起作用”,但在停止并启动模拟器后,它会在每个部分中插入新的重复行。

我认为这与缓存有关,但我非常仔细地遵循上述教程,并设置identificationAttributes来识别唯一记录(例如,不会有效地消除ALL属性我没有指定一个足够唯一的密钥,仅用于调试目的?)。我已经尝试更改缓存名称,将其设置为nil,但它会一直插入重复项。我经常重置模拟器,只是为了开始干净的石板。

如果重要,我会根据教程在getObjectsAtPath中调用viewDidLoad。我对RestKit如何工作的理解是这样做是可以的,因为它足够聪明地推断不需要更新,因为记录都是相同的。

修改

我已将identificationAttributes设置为两个整数属性的数组,这些属性确实可以确定唯一记录。

我还有managedObjectCache

// Seal the deal

[managedStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"CCTDB.sqlite"];

NSError *error;

NSPersistentStore *persistentStore =
[managedStore
 addSQLitePersistentStoreAtPath:storePath
 fromSeedDatabaseAtPath:nil
 withConfiguration:nil
 options:@{
    NSMigratePersistentStoresAutomaticallyOption:@YES,
    NSInferMappingModelAutomaticallyOption:@YES
 }
 error:&error];

NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

[managedStore createManagedObjectContexts];

managedStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedStore.persistentStoreManagedObjectContext];

编辑1

这是第二次

的日志输出
(2013-07-11 03:18:29.961 cocoaclinical[18773:3d07] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'EMDisease': {
    diseaseId =     {
        isPrimitive = 0;
        keyValueCodingClass = NSNumber;
        name = diseaseId;
    };
    diseaseIdValue =     {
        isPrimitive = 1;
        keyValueCodingClass = NSNumber;
        name = diseaseIdValue;
    };
    name =     {
        isPrimitive = 0;
        keyValueCodingClass = NSString;
        name = name;
    };
    subDiseaseId =     {
        isPrimitive = 0;
        keyValueCodingClass = NSNumber;
        name = subDiseaseId;
    };
    subDiseaseIdValue =     {
        isPrimitive = 1;
        keyValueCodingClass = NSNumber;
        name = subDiseaseIdValue;
    };
}
2013-07-11 03:18:29.975 cocoaclinical[18773:3f03] I restkit.core_data:RKInMemoryManagedObjectCache.m:94 Caching instances of Entity 'EMDisease' by attributes 'diseaseId, subDiseaseId'
2013-07-11 03:18:29.983 cocoaclinical[18773:3f03] T restkit.core_data:RKInMemoryManagedObjectCache.m:127 Cached 31 objects
2013-07-11 03:18:29.984 cocoaclinical[18773:3f03] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
    DiseaseSystemIdMember = 161;
    DisplayNameMember = "Acute Lymphocytic Leukemia";
    SubDiseaseSystemIdMember = 1886;
} with mapping <RKEntityMapping:0x96c2850 objectClass=EMDisease propertyMappings=(
    "<RKAttributeMapping: 0x96ba060 SubDiseaseSystemIdMember => subDiseaseId>",
    "<RKAttributeMapping: 0xb58df40 DisplayNameMember => name>",
    "<RKAttributeMapping: 0xb58df70 DiseaseSystemIdMember => diseaseId>"
)>
2013-07-11 03:18:29.984 cocoaclinical[18773:3f03] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
2013-07-11 03:18:29.985 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: <RKMappingOperation 0xb5b7820> for 'EMDisease' object. Mapping values from object {
    DiseaseSystemIdMember = 161;
    DisplayNameMember = "Acute Lymphocytic Leukemia";
    SubDiseaseSystemIdMember = 1886;
} to object <EMDisease: 0xb5b7ee0> (entity: EMDisease; id: 0xb5b7f80 <x-coredata:///EMDisease/t8DD8EE18-C798-468F-9E03-C6A3C724AA772> ; data: {
    diseaseId = 161;
    name = nil;
    subDiseaseId = 1886;
}) with object mapping (null)
2013-07-11 03:18:30.027 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'SubDiseaseSystemIdMember' to 'subDiseaseId'
2013-07-11 03:18:30.028 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'SubDiseaseSystemIdMember to keyPath 'subDiseaseId' -- value is unchanged (1886)
2013-07-11 03:18:30.029 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'DisplayNameMember' to 'name'
2013-07-11 03:18:30.029 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'DisplayNameMember' to 'name'. Value: Acute Lymphocytic Leukemia
2013-07-11 03:18:30.030 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'DiseaseSystemIdMember' to 'diseaseId'
2013-07-11 03:18:30.030 cocoaclinical[18773:3f03] T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'DiseaseSystemIdMember to keyPath 'diseaseId' -- value is unchanged (161)
2013-07-11 03:18:30.031 cocoaclinical[18773:3f03] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully...

1 个答案:

答案 0 :(得分:1)

你需要设置identificationAttributes,通常只有一两个属性,而不是一切。您的实体应该有一些唯一标识符。

您还想在管理对象库中添加managedObjectCache。这是允许RestKit使用您的identificationAttributes匹配对象并更新现有项而不是创建新项的部分。