gpt4 book ai didi

ios - RestKit 本地删除对象,仍然出现在映射中

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:44 26 4
gpt4 key购买 nike

我正在为我的应用程序使用 RestKit 0.20.3。它具有离线模式,因此我必须将对象存储在本地核心数据中。

每当从服务器中删除某些对象时,它会在 JSON 中发送 deleted = true 属性,我在映射中使用 deletionPredicate 处理该属性。这是我的映射。

- (RKEntityMapping *)meetingsMapping {

RKEntityMapping *meetingsMapping = [RKEntityMapping mappingForEntityForName:@"DBMeetings" inManagedObjectStore:objectManager.managedObjectStore];
meetingsMapping.setDefaultValueForMissingAttributes = NO;
meetingsMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"shouldBeDeleted = 1"];
[meetingsMapping setModificationAttributeForName:@"updated_at"];
meetingsMapping.identificationAttributes = @[@"id"];

[meetingsMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"title": @"title",

@"start_date_human": @"start_date_human",
@"start_time_human": @"start_time_human",
@"finish_date_human": @"finish_date_human",
@"finish_time_human": @"finish_time_human",

@"lock": @"lock",
@"location": @"location",
@"sample": @"sample",

@"deleted": @"shouldBeDeleted",

@"created_at": @"created_at",
@"updated_at": @"updated_at",
@"follow_up_id": @"follow_up_id",
@"total_topics": @"total_topics",
}];

[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"tags" mapping:[self tagsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"required_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"optional_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"readonly_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"organizer" mapping:[self contactsMapping]];

return meetingsMapping;

它运行良好,属性已从本地 CoreData 中删除。

问题是 getObjectsAtPath 方法仍然返回 mappingResult 参数中的所有对象。

[[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"%@?type=past&page=%i", URL_MEETINGS, pageNo]
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
KLog(@"%@", mappingResult.array); // It is returning all objects, not only non-deleted objects
completionHandler(mappingResult.array, nil);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
completionHandler(nil, error);
}];

假设如果删除了 20 个对象中的 5 个,mappingResult.array 返回 20 个记录而不是 15 个。这 5 个删除的记录没有数据。当我记录 mappingResult.array 时,前 5 条记录打印如下:

2014-03-27 13:10:14.105 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc1cb820> (entity: DBMeetings; id: 0xc1a6a60 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809410> ; data: <fault>): the object has a temporary managed object ID.
2014-03-27 13:10:14.105 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc5b2ed0> (entity: DBMeetings; id: 0xc5d1110 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809416> ; data: <fault>): the object has a temporary managed object ID.
2014-03-27 13:10:14.106 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc58b6a0> (entity: DBMeetings; id: 0xc5d3de0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809417> ; data: <fault>): the object has a temporary managed object ID.
2014-03-27 13:10:14.106 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc5b50e0> (entity: DBMeetings; id: 0xc5d4af0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809419> ; data: <fault>): the object has a temporary managed object ID.
2014-03-27 13:10:14.107 MeetingKing[7851:70b] W restkit.network.core_data:RKManagedObjectRequestOperation.m:125 Unable to refetch managed object <DBMeetings: 0xc1ed5a0> (entity: DBMeetings; id: 0xc1c84e0 <x-coredata://7B9FC7F6-C0E9-4DF5-91AC-D7C49F54B4F5/DBMeetings/t2915CF2E-2D7D-4E29-8B39-B1C51151809422> ; data: <fault>): the object has a temporary managed object ID.

如果我检查数据库,只有 15 条记录。没关系。

如果我明确地从数据库中获取,它只返回 15 条记录,没关系

但是在getObjectsAtPath回调中,mappingResult.array参数返回了20条记录。那不行。 :(

问题是 mappingResult.arraycount 变为 20 而我的 UITableView 显示 20 行,其中 5 行没有数据。

RestKit 是否有任何其他回调可以准确返回 15 条记录,这样我就不必显式地从数据库中获取记录?

最佳答案

mappingResult 返回被映射的对象,这是它应该做的。某些项目随后被删除的事实并没有改变这一点 - 事实上,您拥有所需的所有信息,因为您知道所有已映射的对象以及哪些对象因此被删除。

简单的解决方案是使用 NSFetchedResultsController 为您的 TableView 获取数据源。它将正确处理更新、插入和删除,您不需要做额外的工作。

或者,您需要迭代 mappingResult.array 并过滤掉已删除的项目。最好的验证方法是使用支票:

if (item.isDeleted || item.managedObjectContext == nil) {
// remove the now invalid item...
}

关于ios - RestKit 本地删除对象,仍然出现在映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com