1

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

我认为这与缓存有关,但我非常密切地遵循上面的教程,并且我已经设置了identificationAttributes识别唯一记录(例如,添加所有属性不会有效地消除我没有指定的可能性一个足够唯一的密钥,仅用于调试目的?)。我尝试更改缓存名称,将其设置为零,但它不断插入重复项。每隔一段时间,我都会重置模拟器,只是为了重新开始。

如果这很重要,我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...
4

1 回答 1

1

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

您还想将一个添加managedObjectCache到您的托管对象存储中。这是允许 RestKit 使用您的匹配对象identificationAttributes并更新现有项目而不是创建新项目的部分。

于 2013-07-11T07:06:25.133 回答