Skip to content

Instantly share code, notes, and snippets.

@ecentinela
Forked from xslim/NSObject+AssociatedObjects.h
Created December 23, 2011 18:33
Show Gist options
  • Save ecentinela/1515023 to your computer and use it in GitHub Desktop.
Save ecentinela/1515023 to your computer and use it in GitHub Desktop.
Blocks support for RestKit RKClient
#import <Foundation/Foundation.h>
@interface NSObject (AMAssociatedObjects)
- (void)associateValue:(id)value withKey:(void *)key; // Retains value.
- (id)associatedValueForKey:(void *)key;
@end
#import "NSObject+AssociatedObjects.h"
#import <objc/runtime.h>
@implementation NSObject (AMAssociatedObjects)
- (void)associateValue:(id)value withKey:(void *)key
{
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY);
}
- (id)associatedValueForKey:(void *)key
{
return objc_getAssociatedObject(self, key);
}
@end
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
extern NSString *kNDidLoadResponse;
extern NSString *kNDidFailLoadResponse;
@interface RKClient (Blocks) <RKRequestDelegate>
- (RKRequest *)load:(NSString *)resourcePath method:(RKRequestMethod)method params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)requestWithResourcePath:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)get:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)get:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)post:(NSString *)resourcePath params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)put:(NSString *)resourcePath params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
- (RKRequest *)delete:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler;
@end
#import "RKClient+Blocks.h"
#import "NSObject+AssociatedObjects.h"
NSString *kNDidLoadResponse = @"network.response.loaded";
NSString *kNDidFailLoadResponse = @"network.response.failed";
@implementation RKClient (Blocks)
/**
* Return a request object targetted at a resource path relative to the base URL with completion handler. By default the method is set to GET
* All headers set on the client will automatically be applied to the request as well.
*/
- (RKRequest *)requestWithResourcePath:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self get:resourcePath queryParams:nil withCompletionHandler:handler];
}
/**
* Fetch a resource via an HTTP GET with completion handler
*/
- (RKRequest *)get:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self get:resourcePath queryParams:nil withCompletionHandler:handler];
}
- (RKRequest *)load:(NSString *)resourcePath method:(RKRequestMethod)method params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
RKRequest *request = [[RKRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:self];
[self setupRequest:request];
[request autorelease];
request.method = method;
request.params = params;
[request associateValue:(id)handler withKey:@"responseBlock"];
[request send];
return request;
}
/**
* Fetch a resource via an HTTP GET with a dictionary of params with completion handler
*
* Note that this request _only_ allows NSDictionary objects as the params. The dictionary will be coerced into a URL encoded
* string and then appended to the resourcePath as the query string of the request.
*/
- (RKRequest *)get:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self load:resourcePath method:RKRequestMethodGET params:queryParams withCompletionHandler:handler];
}
/**
* Create a resource via an HTTP POST with a set of form parameters with completion handler
*/
- (RKRequest *)post:(NSString *)resourcePath params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self load:resourcePath method:RKRequestMethodPOST params:params withCompletionHandler:handler];
}
/**
* Update a resource via an HTTP PUT
*/
- (RKRequest *)put:(NSString *)resourcePath params:(NSObject<RKRequestSerializable> *)params withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self load:resourcePath method:RKRequestMethodPUT params:params withCompletionHandler:handler];
}
/**
* Destroy a resource via an HTTP DELETE
*/
- (RKRequest *)delete:(NSString *)resourcePath withCompletionHandler:(void (^)(RKResponse *response, NSError *error))handler
{
return [self load:resourcePath method:RKRequestMethodDELETE params:nil withCompletionHandler:handler];
}
#pragma mark - RKRequest delegates
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
void (^handler)(RKResponse *response, NSError *error) = [request associatedValueForKey:@"responseBlock"];
handler(response, nil);
}
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError*)error
{
void (^handler)(RKResponse *response, NSError *error) = [request associatedValueForKey:@"responseBlock"];
handler(nil, error);
}
@end
#import <RestKit/ObjectMapping/RKObjectManager.h>
@interface RKObjectManager (Blocks) <RKObjectLoaderDelegate>
- (RKObjectLoader *)objectLoaderWithResourcePath:(NSString *)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler;
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler;
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath objectMapping:(RKObjectMapping*)objectMapping handler:(void (^)(RKObjectLoader *loader, NSError *error))handler;
@end
#import "RKObjectManager+Blocks.h"
#import "NSObject+AssociatedObjects.h"
@implementation RKObjectManager (Blocks)
- (RKObjectLoader *)objectLoaderWithResourcePath:(NSString *)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler
{
RKObjectLoader* objectLoader = nil;
Class managedObjectLoaderClass = NSClassFromString(@"RKManagedObjectLoader");
if (self.objectStore && managedObjectLoaderClass) {
objectLoader = [managedObjectLoaderClass loaderWithResourcePath:resourcePath objectManager:self delegate:self];
} else {
objectLoader = [RKObjectLoader loaderWithResourcePath:resourcePath objectManager:self delegate:self];
}
[objectLoader associateValue:(id)handler withKey:@"responseBlock"];
return objectLoader;
}
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler
{
RKObjectLoader* loader = [self objectLoaderWithResourcePath:resourcePath handler:handler];
loader.method = RKRequestMethodGET;
[loader send];
return loader;
}
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath objectMapping:(RKObjectMapping*)objectMapping handler:(void (^)(RKObjectLoader *loader, NSError *error))handler
{
RKObjectLoader* loader = [self objectLoaderWithResourcePath:resourcePath handler:handler];
loader.method = RKRequestMethodGET;
loader.objectMapping = objectMapping;
[loader send];
return loader;
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error
{
void (^handler)(RKObjectLoader *loader, NSError *error) = [objectLoader associatedValueForKey:@"responseBlock"];
handler(objectLoader, error);
}
- (void)objectLoaderDidFinishLoading:(RKObjectLoader*)objectLoader
{
void (^handler)(RKObjectLoader *loader, NSError *error) = [objectLoader associatedValueForKey:@"responseBlock"];
handler(objectLoader, nil);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment