-
-
Save ArloL/2505201 to your computer and use it in GitHub Desktop.
Blocks support for RestKit RKClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSObject (AMAssociatedObjects) | |
- (void)associateValue:(id)value withKey:(void *)key; // Retains value. | |
- (id)associatedValueForKey:(void *)key; | |
- (void)removeAssociatedValueForKey:(void *)key; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
-(void)removeAssociatedValueForKey:(void *)key | |
{ | |
[self associateValue:nil withKey:key]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Permission granted by Taras Kalapun on 2012-05-02 2:40 AM MDT | |
From t.kalapun@gmail.com: | |
Hi man! | |
you can use my code any way you want and do whatever you want with it, | |
but it's a bit outdated, | |
check out the latest RestKit 0.10.0 or dev branch, they have blocks support there. | |
-- | |
Taras Kalapun | |
iOS Dev @Xaton | |
http://kalapun.com/hcard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <RestKit/RestKit.h> | |
#import "RKClient+Blocks.h" | |
#import "RKObjectManager+Blocks.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <RestKit/RestKit.h> | |
extern NSString *kNDidLoadResponse; | |
extern NSString *kNDidFailLoadResponse; | |
@interface RKClient (Blocks) <RKRequestDelegate> | |
- (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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
{ | |
RKURL* resourcePathURL = nil; | |
if (method == RKRequestMethodGET) { | |
resourcePathURL = [self.baseURL URLByAppendingResourcePath:resourcePath queryParameters:(NSDictionary *)params]; | |
} else { | |
resourcePathURL = [self.baseURL URLByAppendingResourcePath:resourcePath]; | |
} | |
RKRequest *request = [RKRequest requestWithURL:resourcePathURL]; | |
request.delegate = self; | |
[self configureRequest:request]; | |
request.method = method; | |
if (method != RKRequestMethodGET) { | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <RestKit/RKObjectManager.h> | |
@interface RKObjectManager (Blocks) <RKObjectLoaderDelegate> | |
- (RKObjectLoader *)loaderWithResourcePath:(NSString *)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler; | |
- (id)loaderWithURL:(RKURL *)URL handler:(void (^)(RKObjectLoader *loader, NSError *error))handler; | |
- (void)loadObjectsAtResourcePath:(NSString*)resourcePath withLoaderHandler:(void (^)(RKObjectLoader *loader, NSError *error))handler; | |
- (void)putObject:(id<NSObject>)object handler:(void (^)(RKObjectLoader *loader, NSError *error))handler; | |
- (void)loadObjectsAtResourcePath:(NSString*)resourcePath handler:(void (^)(NSArray * objects, NSError * error))handler; | |
- (void)loadObjectAtResourcePath:(NSString*)resourcePath handler:(void (^)(NSObject * object, NSError * error))handler; | |
- (void)loadDictionaryAtResourcePath:(NSString*)resourcePath handler:(void (^)(NSDictionary * dictionary, NSError * error))handler; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RKObjectManager+Blocks.h" | |
#import "NSObject+AssociatedObjects.h" | |
#import <RestKit/RKManagedObjectLoader.h> | |
@implementation RKObjectManager (Blocks) | |
- (RKObjectLoader *)loaderWithResourcePath:(NSString *)resourcePath handler:(void (^)(RKObjectLoader *loader, NSError *error))handler | |
{ | |
RKURL * URL = [self.baseURL URLByAppendingResourcePath:resourcePath]; | |
return [self loaderWithURL:URL handler:handler]; | |
} | |
- (id)loaderWithURL:(RKURL *)URL handler:(void (^)(RKObjectLoader *loader, NSError *error))handler | |
{ | |
RKObjectLoader * loader = [self loaderWithURL:URL]; | |
loader.delegate = self; | |
[loader associateValue:(id)handler withKey:@"responseBlock"]; | |
return loader; | |
} | |
- (id)loaderForObject:(id<NSObject>)object method:(RKRequestMethod)method handler:(void (^)(RKObjectLoader *loader, NSError *error))handler | |
{ | |
RKObjectLoader * loader = [self loaderForObject:object method:method]; | |
loader.delegate = self; | |
[loader associateValue:(id)handler withKey:@"responseBlock"]; | |
return loader; | |
} | |
- (void)loadObjectsAtResourcePath:(NSString*)resourcePath withLoaderHandler:(void (^)(RKObjectLoader *loader, NSError *error))handler | |
{ | |
RKObjectLoader * loader = [self loaderWithResourcePath:resourcePath handler:handler]; | |
loader.method = RKRequestMethodGET; | |
[loader send]; | |
} | |
-(void)putObject:(id<NSObject>)object handler:(void (^)(RKObjectLoader *, NSError *))handler | |
{ | |
RKObjectLoader * loader = [self loaderForObject:object method:RKRequestMethodPUT handler:handler]; | |
[loader send]; | |
} | |
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error | |
{ | |
void (^handler)(RKObjectLoader *loader, NSError *error) = [objectLoader associatedValueForKey:@"responseBlock"]; | |
[objectLoader removeAssociatedValueForKey:@"responseBlock"]; | |
handler(objectLoader, error); | |
} | |
- (void)objectLoaderDidFinishLoading:(RKObjectLoader*)objectLoader | |
{ | |
void (^handler)(RKObjectLoader *loader, NSError *error) = [objectLoader associatedValueForKey:@"responseBlock"]; | |
if(handler) { | |
handler(objectLoader, nil); | |
} | |
} | |
-(void)loadObjectsAtResourcePath:(NSString *)resourcePath handler:(void (^)(NSArray *, NSError *))handler | |
{ | |
RKObjectLoader * loader = [self loaderWithResourcePath:resourcePath handler:^(RKObjectLoader *loader, NSError *error) { | |
handler([[loader result] asCollection], error); | |
}]; | |
loader.method = RKRequestMethodGET; | |
[loader send]; | |
} | |
-(void)loadObjectAtResourcePath:(NSString *)resourcePath handler:(void (^)(NSObject *, NSError *))handler | |
{ | |
RKObjectLoader * loader = [self loaderWithResourcePath:resourcePath handler:^(RKObjectLoader *loader, NSError *error) { | |
handler([[loader result] asObject], error); | |
}]; | |
loader.method = RKRequestMethodGET; | |
[loader send]; | |
} | |
-(void)loadDictionaryAtResourcePath:(NSString *)resourcePath handler:(void (^)(NSDictionary *, NSError *))handler | |
{ | |
RKObjectLoader * loader = [self loaderWithResourcePath:resourcePath handler:^(RKObjectLoader *loader, NSError *error) { | |
handler([[loader result] asDictionary], error); | |
}]; | |
loader.method = RKRequestMethodGET; | |
[loader send]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment