【问题标题】:Restkit onDidFailWithError never get calledRestkit onDidFailWithError 永远不会被调用
【发布时间】:2012-09-25 18:40:16
【问题描述】:

我正在尝试发布一个带有块的对象。 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];
    }];
}];

【问题讨论】:

    标签: ios restkit


    【解决方案1】:

    如果您无法处理错误,请在 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);
    
            }
    
        };
    

    【讨论】:

    • Myclass 是我不想在网上公开的班级名称。我得到了对象加载成功但无法处理错误。
    • 我刚刚编辑了答案,检查是否可以帮助您调试问题所在。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 2013-10-12
      • 2012-03-27
      • 2013-08-29
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2015-10-15
      相关资源
      最近更新 更多