Restkit + Core Data:在每个应用程序启动时,在UITableView中插入重复的值 [英] Restkit + Core Data: Inserts duplicate values in a UITableView on every app launch

查看:247
本文介绍了Restkit + Core Data:在每个应用程序启动时,在UITableView中插入重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following AlexEdge Tutorial I have encountered the following behavior. I can provide code as requested since there are many lines and I'm not really sure where to look. Basically, it "works" in the sense that the data is properly loaded into the UITableView, but after stopping and starting up the simulator, it inserts new duplicate rows in each section.

我认为这将与缓存有关,但我遵循上面的教程相当密切,我已设置 identifierAttributes 标识唯一的记录(不会,例如,添加ALL属性有效地消除了我没有指定足够唯一的键,只是为了调试的可能性?)。我试图更改缓存名称,将其设置为零,但它保持插入重复。

I figured this would have something to do with caching but I am following the above tutorial pretty closely and I've set the identificationAttributes to identify unique records (wouldn't, for example, adding ALL attributes effectively eliminate the possibility that I've not specified a sufficiently unique key, just for debugging purposes?). I've tried changing the Cache name, setting it nil, but it keeps inserting duplicates. Every so often I reset the simulator just to start on a clean slate.

如果重要的话,我会调用 getObjectsAtPath viewDidLoad 中。我对RestKit的工作方式的理解是,这样做是可以的,因为它足够聪明地推断出没有更新是必要的,因为记录都是一样的。

If it matters, I am calling getObjectsAtPath in viewDidLoad as per the tutorial. My understanding of how RestKit worked was that it was okay to do so because it would be smart enough to infer that no updates were necessary as the records are all the same.

编辑

我已将 identificationAttributes 设置为两个整数属性的数组确定唯一记录。

I have set identificationAttributes to an array of two integer attributes that really do determine a unique record.

我也有一个 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...


推荐答案

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

You do need to set identificationAttributes, generally only one or two attributes though, not everything. Your entities should have some unique identifier.

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

You also want to add a managedObjectCache to your managed object store. This is the part that allows RestKit to match objects using your identificationAttributes and update the existing items instead of creating new ones.

这篇关于Restkit + Core Data:在每个应用程序启动时,在UITableView中插入重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆