绝不会调用Restkit onDidFailWithError

时间:2012-09-25 18:40:17

标签: ios restkit

我正在尝试使用块发布对象。 OnDidFailWithError永远不会被召唤,它会爆炸。

这是我得到的错误:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:'+ [MyClass   objectLoader:didFailWithError:]:发送到类

的无法识别的选择器

这是我的代码:

[[RKObjectManager sharedManager] postObject: myObj usingBlock:^(RKObjectLoader *loader){
    loader.targetObject = nil;
    loader.delegate = (id)self;
    loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[MyClass class]];

    loader.onDidFailWithError = ^(NSError *error) {
        NSLog(@"Error %@", [error localizedDescription]);
    };

    loader.onDidLoadObject = ^(id obj) {
        NSLog(@"Comment");
        NSLog(@"%@",obj);
    };

    loader.onDidLoadResponse = ^(RKResponse *response) {
        NSLog(@"Response: %@", [response bodyAsString]);
    };

    loader.onDidLoadObjects=^(NSArray* objects){
        //post notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"finish" object:nil];
    };

    loader.serializationMIMEType = RKMIMETypeJSON; // We want to send this request as JSON
    loader.method = RKRequestMethodPOST;

    loader.serializationMapping = [RKObjectMapping serializationMappingUsingBlock:^(RKObjectMapping* mapping) {
        [mapping mapAttributes:@"field1", @"field2",nil];
    }];
}];

2 个答案:

答案 0 :(得分:0)

如果您无法处理错误,请尝试 onDidLoadResponse

    loader.onDidLoadResponse = ^(RKResponse *response) {

        NSLog(@"Response did arrive");
        if([response statusCode]>299){

            NSError *error = nil;
            id parsedResponse = [NSJSONSerialization JSONObjectWithData:[response body] options:NSJSONWritingPrettyPrinted error:&error];
            NSLog(@"%@",parsedResponse);

        }

    };

答案 1 :(得分:0)

您正在将委托设置为self,这将调用当前控制器上的RestKit委托方法,而不是使用您创建的块。