Restkit + Objective-c - Multiple calls to same web

2019-08-19 04:42发布

问题:

I'm new to restkit and iOS. I'm using xcode 4.2 and RestKit 0.2. I've been developing an iPhone app that communicates with some REST web services that I've created and now I'm facing a problem. A few of my web services are meant to be call more than once. The first request runs with no problem but when I try to make the call again, I get this error:

'NSInternalInconsistencyException', reason: 'Cannot add a request descriptor for the same object class as an existing request descriptor.'

I do understand what this error means but have not found a way to implement the calls that can be reused. I have a feeling I'm doing it wrong. Below is one of the functions that I'm using to call the web service:

- (void) restAddPlayer:(addPlayer*)add block:(void (^)(id))block{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKObjectMapping *categoryRequestMapping = [RKObjectMapping requestMapping];
[categoryRequestMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

RKObjectMapping *reqMapping = [RKObjectMapping requestMapping];
[reqMapping addAttributeMappingsFromDictionary:@{@"plName": @"playerName"}];
[reqMapping addRelationshipMappingWithSourceKeyPath:@"groupInfo" mapping:categoryRequestMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:reqMapping objectClass:[addPlayer class] rootKeyPath:nil];

[objectManager addRequestDescriptor:requestDescriptor];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[objectManager postObject:add path:@"addPlayer"parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                      NSArray* statuses = [mappingResult array];
                     //NSLog(@"Loaded statuses: %@", statuses);
                     // _result = [statuses objectAtIndex:0];
                      block(statuses);

                  }
                 failure:^(RKObjectRequestOperation *operation, NSError *error){
                     NSLog(@"Hit error: %@", error);
                     block(nil);
                 }
    ];
}

I know I'm adding a request descriptor and when I call the function again it's trying to add another and thus the error. What I would like to know is how to handle this so I can call the web service more than once with different information in the object being post.

Thanks to all in advance.

Regards

回答1:

Everything in this routine, except for the "postObject" call should probably be somewhere else, like in your app-delegate when the app starts up.

You're setting up mappings, relationships, a request descriptor. You don't need to keep specifying them for each call. Just set them up once, and then when you're ready, just call "postObject". The same probably goes for your serializationType as well -- unless some REST endpoints return XML and others return JSON, you're probably OK to set this once, and forget about it.