您好我已将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
{
"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
请求方法。
我做错了吗?或者需要做一些其他事情来删除孤儿对象。任何帮助表示赞赏。
答案 0 :(得分:11)
最后通过逐步调试RestKit解决了这个问题,并在RKManagedObjectRequestOperation.m
if (! [[self.HTTPRequestOperation.request.HTTPMethod uppercaseString] isEqualToString:@"GET"]) {
RKLogDebug(@"Skipping deletion of orphaned objects: only performed for GET requests.");
return YES;
}
哦!当请求方法为POST
时,RestKit不会删除孤立对象。我也改变了接受GET
请求的Web服务,它完美地运行了。
其次我正在使用predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id == %@", [DBUser currentUser].user_id];
删除除当前用户以外的所有对象是错误的,我必须追加NOT
运算符,就像这样
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id != %@", [DBUser currentUser].user_id];
所以predicate
并不意味着你想要什么,但这是你不想要的。