Reputation: 21
It possible, to add couple of response mapping for same pathPattern?
I have next situation:
Get request: RKEntityMapping * restDayMapping = [RestDay entityMapping]; RKResponseDescriptor * restDayDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:restDayMapping pathPattern:@"program/restdays" keyPath:@"rest_days" statusCodes:statusCodes]; [manager addResponseDescriptor:restDayDescriptor]; Put request: RKObjectMapping * restDayPutMapping = [RKObjectMapping requestMapping]; [restDayPutMapping addAttributeMappingsFromDictionary:@{@"dayNumber" : @"restDays.dayNumber"}]; RKRequestDescriptor * restDayPutDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[restDayPutMapping inverseMapping] objectClass:[UserProgram class] rootKeyPath:@"rest_days"]; [manager addRequestDescriptor:restDayPutDescriptor];
So both mappings send by same path pattern: @"program/restdays" But for first GET request server side return json:
{"rest_days":[{"dayNumber":1},{"dayNumber":4},{"dayNumber":2}]}.
For second PUT request, json:
{"success" : "true"}
How I must configurate my restkit mappings? Thank you.
EDIT: Upgrade restkit library and found wonderful feature, method was depricated:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping pathPattern:(NSString *)pathPattern keyPath:(NSString *)keyPath statusCodes:(NSIndexSet *)statusCodes
And added new one:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping method:(RKRequestMethod)method pathPattern:(NSString *)pathPattern keyPath:(NSString *)keyPath statusCodes:(NSIndexSet *)statusCodes
So now I can set different mappings for different request methods. Thanks everyone for help.
Upvotes: 1
Views: 924
Reputation: 21
Upgraded restkit library (08/29/13) and found wonderful feature, method was depricated:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping pathPattern:(NSString *)pathPattern keyPath:(NSString *)keyPath statusCodes:(NSIndexSet *)statusCodes
And added new one:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping method:(RKRequestMethod)method pathPattern:(NSString *)pathPattern keyPath:(NSString *)keyPath statusCodes:(NSIndexSet *)statusCodes
So now I can set different mappings for different request methods. Thanks everyone for help.
Upvotes: 1
Reputation: 119031
You can have 2 different mappings for the same path pattern so long as they are to be used for different purposes (like GET and PUT). RestKit needs to be able to tell which one to use, as long as it can everything's fine.
There are 2 ways to do a PUT though:
objectManager putObject:...
This will automatically try to map the response onto the source object. So if you expect a status response back it isn't going to work well. Alternatively you can use RestKit to do the serialisation for you:
NSError* error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:&error];
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
And then PUT the results yourself (using RestKit classes or NSURLConenction
).
Your best option may be to use RKObjectRequestOperation
, technically you won't be using the mappings against the same path pattern but you can use your mappings both to create the request and to process the response.
Upvotes: 2