【发布时间】:2013-11-25 22:23:30
【问题描述】:
您好,我已将 RestKit 从 0.10.2 更新到 0.20.3。 现在更新后,当 Web 服务中缺少对象时,RestKit 不会从本地存储中删除它们。我知道 RestKit 0.20.x 支持它,但我无法配置它。我按照这里给出的例子。 http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html#overview
我也在 Stackoverflow 上关注了类似的问题。但目前还没有公认的答案。
这是我的 Json
{
"status": "Success",
"member_info": [
{
"family_id": "1",
"user_id": "1"
},
{
"family_id": "2",
"user_id": "1"
},
{
"family_id": "3",
"user_id": "1"
}
]
}
返回特定用户的所有家庭成员。以上 Json 用于user_id = 1。现在我想如果为user_id = 2 发送请求,那么应该从本地存储中删除所有之前的 3 个对象,因为它们现在从 Json 中丢失了。这是user_id = 2的新Json
{
"status": "Success",
"member_info": [
{
"family_id": "4",
"user_id": "2"
},
{
"family_id": "5",
"user_id": "2"
}
]
}
这就是我创建映射的方式。
[objectManager addResponseDescriptorsFromArray:@[
[RKResponseDescriptor responseDescriptorWithMapping:[self connectionsMapping]
method:RKRequestMethodPOST
pathPattern:@"familylist"
keyPath:@"member_info" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"familylist"];
NSDictionary *argsDict = nil;
BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
if (match) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"DBConnections"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id == %@", [DBUser currentUser].user_id];
fetchRequest.predicate = predicate;
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES] ];
return fetchRequest;
}
return nil;
}];
- (RKEntityMapping *)connectionsMapping {
RKEntityMapping *connectionsMapping = [RKEntityMapping mappingForEntityForName:@"DBConnections" inManagedObjectStore:objectManager.managedObjectStore];
connectionsMapping.setDefaultValueForMissingAttributes = NO;
connectionsMapping.identificationAttributes = @[@"family_id"];
[connectionsMapping addAttributeMappingsFromDictionary:@{
@"family_id": @"family_id",
@"user_id": @"user_id",
}];
return connectionsMapping;
}
在谓词中我使用[DBUser currentUser].user_id,它返回当前登录的user_id。这是我正在调用网络服务
[[RKObjectManager sharedManager] postObject:nil path:@"familylist" parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Objects are %@", mappingResult.array);
}];
我使用postObject 而不是getObjectsAtPath,因为服务器期待POST 请求方法。
我做错了吗?或者需要做其他事情来删除孤立对象。任何帮助表示赞赏。
【问题讨论】:
-
你调试
[DBUser currentUser].user_id的值了吗?谓词实际上不应该是必需的,您是否尝试过不使用谓词? -
谢谢@Wain,我在找你 :),我确实调试了谓词的值,我很好,让我尝试不使用谓词。
-
没有谓词还是一样,之前的数据不会被删除。
-
我想我需要将“deletesOrphanedObjects”设置为true,但不知道该怎么做,关于这个属性有什么建议吗?
标签: ios objective-c json restkit restkit-0.20