我正在为我的应用程序使用 RestKit 0.20.3。它具有离线模式,所以我必须将对象存储在本地核心数据中。
每当从服务器中删除某些对象时,它会以 JSON 格式发送 deleted = true 属性,我在与 deletionPredicate 的映射中处理该属性。
这是我的 map 。
- (RKEntityMapping *)meetingsMapping {
RKEntityMapping *meetingsMapping = [RKEntityMapping mappingForEntityForName"DBMeetings" inManagedObjectStorebjectManager.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.
如果我 checkin 数据库,则只有 15 条记录。没关系。
如果我明确地从数据库中获取,它只返回 15 条记录,没关系
但是在getObjectsAtPath 回调中,mappingResult.array 参数返回20条记录。那不行。
问题是 mappingResult.array 的 count 变成 20,而我的 UITableView 显示 20 行,其中 5 行没有数据。
RestKit 是否有任何其他回调可以准确返回 15 条记录,这样我就不必从数据库中显式获取记录?
Best Answer-推荐答案 strong>
mappingResult 正在返回被映射的对象,这是它应该做的。一些项目随后被删除的事实并没有改变这一点 - 实际上,您拥有所需的所有信息,因为您知道所有已映射的对象以及哪些对象因此被删除。
简单的解决方案是使用 NSFetchedResultsController 来获取表格 View 的数据。它将正确处理更新、插入和删除,您不需要做额外的工作。
或者,您需要迭代 mappingResult.array 并过滤掉已删除的项目。最好的验证方法是使用支票:
if (item.isDeleted || item.managedObjectContext == nil) {
// remove the now invalid item...
}
关于ios - RestKit本地删除对象,依然出现在mapping中,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/22682037/
|