Restkit - [RKManagedObjectRequestOperation deleteTargetObject:]未调用

时间:2014-07-29 19:47:08

标签: ios restkit http-delete

我使用以下命令使用RestKit(0.23.1)删除对象:

[self.objectManager deleteObject:myObject path:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"success deleting myObject");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"failure deleting myObject");
}];

这会创建正确的DELETE请求,服务器返回200状态代码。但是,不会从Core Data中删除本地数据。如果在触发上面的成功块之后我对已删除的对象进行了提取,则提取将返回应该删除的对象。

The documentation读到:

  

RKManagedObjectRequestOperation为DELETE添加了特殊行为   要求。检索成功(2xx状态代码)响应后   一个DELETE,该操作将调用deleteObject:with操作   托管对象上下文中的targetObject。这将删除   目标对象从本地存储中成功结合   删除了远程表示。

看起来像-[RKManagedObjectRequestOperation deleteTargetObject:]似乎没有被调用。它似乎应该由-[RKManagedObjectRequestOperation willFinish]调用。

似乎-[RKManagedObjectRequestOperation willFinish]中的gating if语句可能存在逻辑错误。

- (void)willFinish
{
    NSMutableIndexSet *deleteableStatusCodes = [NSMutableIndexSet indexSet];
    [deleteableStatusCodes addIndex:404]; // Not Found
    [deleteableStatusCodes addIndex:410]; // Gone
    if (self.error && self.targetObjectID
        && [[[self.HTTPRequestOperation.request HTTPMethod] uppercaseString] isEqualToString:@"DELETE"]
        && [deleteableStatusCodes containsIndex:self.HTTPRequestOperation.response.statusCode]) {
        NSError *error = nil;
        if (! [self deleteTargetObject:&error]) {
            RKLogWarning(@"Secondary error encountered while attempting to delete target object in response to 404 (Not Found) or 410 (Gone) status code: %@", error);
            self.error = error;
        } else {
            if (! [self saveContext:&error]) {

            } else {
                // All good, clear any errors
                self.error = nil;
            }
        }
    }
}

似乎-[RKManagedObjectRequestOperation willFinish]应该如下:

- (void)willFinish
{
    NSMutableIndexSet *deleteableStatusCodes = [NSMutableIndexSet indexSet];
    [deleteableStatusCodes addIndex:404]; // Not Found
    [deleteableStatusCodes addIndex:410]; // Gone
    [deleteableStatusCodes addIndexes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    if (self.error == nil
        && self.targetObjectID
        && [[[self.HTTPRequestOperation.request HTTPMethod] uppercaseString] isEqualToString:@"DELETE"]
        && [deleteableStatusCodes containsIndex:self.HTTPRequestOperation.response.statusCode]) {
        NSError *error = nil;
        if (! [self deleteTargetObject:&error]) {
            RKLogWarning(@"Secondary error encountered while attempting to delete target object in response to 404 (Not Found) or 410 (Gone) status code: %@", error);
            self.error = error;
        } else {
            if (! [self saveContext:&error]) {

            } else {
                // All good, clear any errors
                self.error = nil;
            }
        }
    }
}

为了总结这些更改,我将成功的状态代码添加到deleteableStatusCodes索引集并检查以确保self.error等于nil。

我的逻辑改变了,还是我在这里误解了一些RestKit约定?看起来我的逻辑更改与文档中引用的语句一致。

1 个答案:

答案 0 :(得分:1)

你在错误的地方寻找。见行:

success = [weakSelf deleteTargetObject:&error];

performMappingOnResponseWithCompletionBlock:中的RKManagedObjectRequestOperation方法检查成功状态并删除对象。

您编辑的代码是处理错误响应,在一个漂亮的REST接口中,这意味着与DELETE成功响应相同。