我在我的应用程序中使用RestKit 0.20.3。它具有离线模式,因此我必须将对象存储在本地coredata中。
每当从服务器删除某些对象时,它会在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.
如果我签入DB,则只有15条记录。没关系。
如果我从DB显式提取,它只返回15条记录,那就没问题
但在getObjectsAtPath
回调中,mappingResult.array
参数返回20条记录。那不行。 :(
问题是count
的{{1}}变为20,我的mappingResult.array
显示20行,其中5行没有数据。
RestKit是否还有其他回调准确返回15条记录,以便我不必显式从DB中获取记录?
答案 0 :(得分:2)
mappingResult
正在返回已映射的对象,这就是它应该做的事情。随后删除某些项目的事实并没有改变这一点 - 事实上,您知道所有需要的信息,因为您知道所有已映射的对象以及哪些对象已被删除。
简单的解决方案是使用NSFetchedResultsController
为您的表格视图提供数据。它将正确处理更新,插入和删除,您无需再做其他工作。
或者,您需要迭代mappingResult.array
并过滤掉已删除的项目。验证的最佳方法是使用支票:
if (item.isDeleted || item.managedObjectContext == nil) {
// remove the now invalid item...
}