Skip to content

Instantly share code, notes, and snippets.

@emcmanus
Created April 14, 2012 02:22
Show Gist options
  • Save emcmanus/2381616 to your computer and use it in GitHub Desktop.
Save emcmanus/2381616 to your computer and use it in GitHub Desktop.
Block-based API Wrapper for Restkit (Requires A2DynamicDelegate)
//
// RKObjectManager+Blocks.h
//
// Created by Ed McManus for Yardsale Inc. on 3/27/12
// Learn more at https://getyardsale.com
//
// Requires A2DynamicDelegate https://github.com/pandamonia/A2DynamicDelegate
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
typedef void(^RKSuccessBlock)(RKObjectLoader* loader, NSArray* objects);
typedef void(^RKFailureBlock)(RKObjectLoader* loader, NSError* error);
@interface RKObjectManager (Blocks)
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
- (RKObjectLoader*)objectLoaderForObject:(id<NSObject>)object method:(RKRequestMethod)method onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
- (RKObjectLoader*)getObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
- (RKObjectLoader*)postObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
- (RKObjectLoader*)putObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
- (RKObjectLoader*)deleteObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
@end
//
// RKObjectManager+Blocks.m
//
// Created by Ed McManus for Yardsale Inc. on 3/27/12
// Learn more at https://getyardsale.com
//
// Requires A2DynamicDelegate https://github.com/pandamonia/A2DynamicDelegate
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <objc/message.h>
#import <objc/runtime.h>
#import "RKObjectManager+Blocks.h"
@implementation RKObjectManager (Blocks)
- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
NSString *protocolName = @"RKObjectLoaderDelegate";
Protocol *protocol = objc_getProtocol(protocolName.UTF8String);
RKObjectLoader * loader = [self objectLoaderWithResourcePath:resourcePath delegate:nil];
A2DynamicDelegate * dynamicDelegate = [loader dynamicDelegateForProtocol:protocol];
[dynamicDelegate implementMethod:@selector(objectLoader:didLoadObjects:) withBlock:successBlock];
[dynamicDelegate implementMethod:@selector(objectLoader:didFailWithError:) withBlock:failureBlock];
[loader setDelegate:(NSObject<RKObjectLoaderDelegate>*)dynamicDelegate];
[loader send];
return loader;
}
- (RKObjectLoader*)objectLoaderForObject:(id<NSObject>)object method:(RKRequestMethod)method onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
NSString *protocolName = @"RKObjectLoaderDelegate";
Protocol *protocol = objc_getProtocol(protocolName.UTF8String);
RKObjectLoader* loader = [self objectLoaderForObject:object method:method delegate:nil];
A2DynamicDelegate * dynamicDelegate = [loader dynamicDelegateForProtocol:protocol];
[dynamicDelegate implementMethod:@selector(objectLoader:didLoadObjects:) withBlock:successBlock];
[dynamicDelegate implementMethod:@selector(objectLoader:didFailWithError:) withBlock:failureBlock];
[loader setDelegate:(NSObject<RKObjectLoaderDelegate>*)dynamicDelegate];
[loader send];
return loader;
}
- (RKObjectLoader*)getObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
return [self objectLoaderForObject:object method:RKRequestMethodGET onSuccess:successBlock onFailure:failureBlock];
}
- (RKObjectLoader*)postObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
return [self objectLoaderForObject:object method:RKRequestMethodPOST onSuccess:successBlock onFailure:failureBlock];
}
- (RKObjectLoader*)putObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
return [self objectLoaderForObject:object method:RKRequestMethodPUT onSuccess:successBlock onFailure:failureBlock];
}
- (RKObjectLoader*)deleteObject:(id<NSObject>)object onSuccess:(RKSuccessBlock)successBlock onFailure:(RKFailureBlock)failureBlock;
{
return [self objectLoaderForObject:object method:RKRequestMethodDELETE onSuccess:successBlock onFailure:failureBlock];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment