Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make it possible to access willMapDeserializedResponseBlock to modify the deserialized JSON response when you are using RKObjectManager.
@interface RKObjectManager (ModifyJSONRepsonse)
- (void)getObjectsAtPath:(NSString *)path
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure;
- (void)getObjectsAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure;
- (void)getObjectsAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure;
@end
@implementation RKObjectManager (ModifyJSONRepsonse)
- (void)getObjectsAtPath:(NSString *)path
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(path);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.willMapDeserializedResponseBlock = modifyResponseBlock;
[self enqueueObjectRequestOperation:operation];
}
- (void)getObjectsAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodGET];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.willMapDeserializedResponseBlock = modifyResponseBlock;
[self enqueueObjectRequestOperation:operation];
}
- (void)getObjectsAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
modifyResponseBlock:(id (^)(id deserializedResponseBody))modifyResponseBlock
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method == RKRequestMethodGET, @"Expected route named '%@' to specify a GET, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.willMapDeserializedResponseBlock = modifyResponseBlock;
[self enqueueObjectRequestOperation:operation];
}
@end
@amitmalhotra
Copy link

amitmalhotra commented Jan 28, 2014

Since URLWithRoute: method on RKObjectManager is private, it seems inaccessible to the category. How are you accessing it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment