我只是使用restkit patchobject更新内容..当第一次调用方法调用时它会导致成功。但在第二次调用相同的方法时,应用程序崩溃与NSInternal不一致错误。无法为同一个类添加adddescriptor。谢谢。下面的链接也有同样的问题,但我不知道如何解决。
Restkit + Objective-c - Multiple calls to same web service
这是我的方法代码
-(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");
}];
}
答案 0 :(得分:1)
您面临的问题是因为您多次添加相同的请求描述符。您应该只设置一次所有请求和响应描述符,例如,在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]];
}
但是在某些情况下,您仍然希望添加多个请求描述符,然后通过REST Kit的作者回答的动态映射来完成。请参阅以下链接。
Adding two request descriptors for a given class in Restkit 0.2
我希望这会有所帮助。
答案 1 :(得分:0)
您无法两次为同一个密钥添加描述符。您还没有添加任何代码,但我知道您在调用'patch'的方法中添加了所有映射和描述符。不要......
将您的配置与您的使用分开。将所有映射和描述符设置代码放在一个方法中,并在执行任何其他操作之前调用它一次。然后,当你想要'补丁'时,调用一个不同的方法来执行该操作,并且不包含任何其他映射。