10

嗨,我已将 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 中丢失了。这是新的Jsonuser_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请求方法。我做错了吗?或者需要做其他事情来删除孤立对象。任何帮助表示赞赏。

4

1 回答 1

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;
    }

哦!当请求方法为 时,RestKit 不会删除孤立对象POST。我也更改了接受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并不意味着你想要什么,而是你不想要什么。

于 2013-11-13T11:59:44.290 回答