RestKit本地删除对象,依然出现在mapping中
Posted
技术标签:
【中文标题】RestKit本地删除对象,依然出现在mapping中【英文标题】:RestKit local deletion of objects, still appears in mapping 【发布时间】:2014-03-27 08:30:40 【问题描述】:我正在为我的应用程序使用 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.array
的count
变成了20,而我的UITableView
显示20行,其中5行没有数据。
RestKit 是否有任何其他回调可以准确返回 15 条记录,这样我就不必从数据库中显式获取记录?
【问题讨论】:
【参考方案1】:mappingResult
正在返回映射的对象,这是它应该做的。一些项目随后被删除的事实并没有改变这一点 - 实际上,您拥有所需的所有信息,因为您知道所有已映射的对象以及哪些对象因此被删除。
简单的解决方案是使用NSFetchedResultsController
来获取表格视图的数据。它将正确处理更新、插入和删除,您不需要做额外的工作。
或者,您需要迭代 mappingResult.array
并过滤掉已删除的项目。最好的验证方法是使用支票:
if (item.isDeleted || item.managedObjectContext == nil)
// remove the now invalid item...
【讨论】:
感谢@wain,我正在使用 MagicalRecords,并用它再次从数据库中获取数据。它正在返回正确的记录。我想知道是否有其他方法可以实现这一目标。以上是关于RestKit本地删除对象,依然出现在mapping中的主要内容,如果未能解决你的问题,请参考以下文章
当它们没有出现在 JSON 中时,Restkit 删除 Coredata 中的对象
使用 Restkit 在 Core Data 中删除后保存对象使其崩溃
在 Restkit 执行映射操作之前删除 Core data 中的现有对象