1

我只是使用restkit patchobject 更新内容。当第一次调用方法调用时,它会导致成功。但是在第二次调用相同的方法时,应用程序崩溃并出现NSInternal 不一致错误。无法为同一类添加adddescriptor。谢谢。下面的链接也有同样的问题,但我不知道如何解决。

Restkit + Objective-c - 多次调用同一个 Web 服务

这是我的方法代码

-(void)setContact:(int)_orgID :(int)_personID :(Person *)p1
{
    AddressScreenViewController *addressView= [[AddressScreenViewController alloc]init];
    addressView.mobileno = p1.mobile_phone;
    addressView.workno = p1.work_phone;
    addressView.homeno = p1.home_phone;
    addressView.address1=p1.address1;
    addressView.address2=p1.address2;
    addressView.city=p1.city;
    addressView.zip=p1.zip;
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    LoginAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];

RKObjectMapping *personRequestMapping = [RKObjectMapping requestMapping];


[personRequestMapping addAttributeMappingsFromDictionary:@{ @"mobileno" : @"phone_numbers.mobile_number", @"workno" : @ "phone_numbers.work_number" , @"homeno" :@"phone_numbers.home_number",@"address1":@"mailing_address.address1",@"address2":@"mailing_address.address2",@"city":@"mailing_address.city",@"zip":@"mailing_address.zip"}];
    RKLogConfigureByName("RestKit", RKLogLevelWarning);
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
   RKRequestDescriptor *requestDescriptor =[RKRequestDescriptor requestDescriptorWithMapping:personRequestMapping objectClass:[AddressScreenViewController class] rootKeyPath:@"person"];

[objectManager  addRequestDescriptor:requestDescriptor];

NSString * orgPath = [NSString stringWithFormat:myurl];

[objectManager patchObject:addressView path:orgPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
 {

     NSLog(@"result: %@", result);

 } failure:^(RKObjectRequestOperation *operation, NSError *error) {
     NSLog(@"failuer function");
 }];

}
4

2 回答 2

1

您面临的问题是因为您多次添加相同的请求描述符。您应该只设置一次所有请求和响应描述符,例如,在应用程序委托中。

+ (void)setupDescriptors {
    RKObjectManager *objectManager = [[AppDelegate  appDelegate] objectManager];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    // Add Request Descriptors
    //

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Human rkEntityMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"human" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    RKRequestDescriptor *userRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[User rkObjectMappingForRequest:YES] objectClass:[User class] rootKeyPath:@"user" method:RKRequestMethodAny];
    RKRequestDescriptor *signupUserRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[User rkObjectMappingForSignupRequest:YES] objectClass:[User class] rootKeyPath:@"user" method:RKRequestMethodAny];


    [objectManager addRequestDescriptorsFromArray:@[signupUserRequestDescriptor,userRequestDescriptor]];

    RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[User rkEntityMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"user" statusCodes:statusCodes];


    [objectManager addResponseDescriptorsFromArray:@[userResponseDescriptor,responseDescriptor]];
}

但是在某些情况下,您仍然希望添加多个请求描述符,然后通过 REST Kit 的作者回答的动态映射来完成。请参阅以下链接。

在 Restkit 0.2 中为给定类添加两个请求描述符

我希望这有帮助。

于 2014-04-24T14:17:15.510 回答
0

您不能为同一个键添加两次描述符。您没有添加任何代码,但我知道您在调用“补丁”的方法中添加了所有映射和描述符。不...

将您的配置与您的使用分开。将所有映射和描述符设置代码放入一个方法中,并在执行其他任何操作之前调用它一次。然后,当您想要“修补”时,调用一个不同的方法,它只是这样做并且不包含任何额外的映射。

于 2013-07-04T19:44:07.497 回答