Search code examples
iosrestkit

cannot add a requestdescriptor for a same class restkit ios throws error nsinternal inconsistency error


i just update a content using restkit patchobject..when first time the method call invoked it leads success.but during second time call the same method ,the app crashes with NSInternal inconsistecy error.Cannot add adddescriptor for same class.thanks. below link also have same problem,but i dont know how to solve.

Restkit + Objective-c - Multiple calls to same web service

here is my method code

-(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");
 }];

}

Solution

  • The issue you are facing is because you are adding same request descriptor multiple times. You should set all request and response descriptor only once for example, in app delegate.

    + (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]];
    }
    

    But in some scenarios, you still want to add multiple request descriptor then this is done by dynamic mapping as answered by Author of REST Kit. Please see following link.

    Adding two request descriptors for a given class in Restkit 0.2

    I hope this helps.