Skip to content

Instantly share code, notes, and snippets.

@mflint
Created November 21, 2011 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mflint/1384099 to your computer and use it in GitHub Desktop.
Save mflint/1384099 to your computer and use it in GitHub Desktop.
LRResty timeout workaround
//
// LRRestyClient+FailureBlock.h
//
#import <Foundation/Foundation.h>
#import <LRResty/LRResty.h>
#import "RVRestyClientBlockDelegate.h"
@interface LRRestyClient (FailureBlock)
- (LRRestyRequest *)get:(NSString *)urlString withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)block;
- (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)block;
- (LRRestyRequest *)post:(NSString *)urlString payload:(id)payload withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)block;
- (LRRestyRequest *)delete:(NSString *)urlString withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock;
- (LRRestyRequest *)put:(NSString *)urlString payload:(id)payload withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock;
@end
//
// LRRestyClient+FailureBlock.m
//
#import "LRRestyClient+FailureBlock.h"
@implementation LRRestyClient (FailureBlock)
- (LRRestyRequest *)get:(NSString *)urlString withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock {
return [self get:urlString parameters:nil headers:nil withBlock:block andErrorBlock:errorBlock];
}
- (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock {
return [HTTPClient GET:[NSURL URLWithString:urlString] parameters:parameters headers:headers delegate:[RVRestyClientBlockDelegate delegateWithBlock:block andErrorBlock:errorBlock]];
}
- (LRRestyRequest *)post:(NSString *)urlString payload:(id)payload withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock {
return [HTTPClient POST:[NSURL URLWithString:urlString] payload:payload headers:nil delegate:[RVRestyClientBlockDelegate delegateWithBlock:block andErrorBlock:errorBlock]];
}
- (LRRestyRequest *)delete:(NSString *)urlString withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock {
return [HTTPClient DELETE:[NSURL URLWithString:urlString] headers:nil delegate:[RVRestyClientBlockDelegate delegateWithBlock:block andErrorBlock:errorBlock]];
}
- (LRRestyRequest *)put:(NSString *)urlString payload:(id)payload withBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock {
return [HTTPClient PUT:[NSURL URLWithString:urlString] payload:payload headers:nil delegate:[RVRestyClientBlockDelegate delegateWithBlock:block andErrorBlock:errorBlock]];
}
@end
//
// RVRestyClientBlockDelegate.h
//
#import <Foundation/Foundation.h>
#import <LRResty/LRResty.h>
typedef void (^RVRestyErrorBlock)(NSError *error);
@interface RVRestyClientBlockDelegate : NSObject <LRRestyRequestDelegate>
{
LRRestyResponseBlock block;
RVRestyErrorBlock errorBlock;
}
+ (void)setDispatchesOnMainQueue:(BOOL)shouldDispatchOnMainQueue;
+ (id)delegateWithBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock) errorBlock;
- (id)initWithBlock:(LRRestyResponseBlock)theBlock andErrorBlock:(RVRestyErrorBlock) theErrorBlock;
@end
//
// RVRestyClientBlockDelegate.m
//
#import "RVRestyClientBlockDelegate.h"
@implementation RVRestyClientBlockDelegate
static BOOL _shouldDispatchOnMainQueue = YES;
+ (void)setDispatchesOnMainQueue:(BOOL)shouldDispatchOnMainQueue
{
_shouldDispatchOnMainQueue = shouldDispatchOnMainQueue;
}
+ (id)delegateWithBlock:(LRRestyResponseBlock)block andErrorBlock:(RVRestyErrorBlock)errorBlock;{
return [[[self alloc] initWithBlock:block andErrorBlock:errorBlock] autorelease];
}
- (id)initWithBlock:(LRRestyResponseBlock)theBlock andErrorBlock:(RVRestyErrorBlock)theErrorBlock;{
if ((self = [super init])) {
block = [theBlock copy];
errorBlock = [theErrorBlock copy];
}
return self;
}
- (void)dealloc{
[block release];
[errorBlock release];
[super dealloc];
}
- (void)restyRequest:(LRRestyRequest *)request didFinishWithResponse:(LRRestyResponse *)response
{
if (block) {
if (_shouldDispatchOnMainQueue) {
dispatch_async(dispatch_get_main_queue(), ^{
block(response);
});
}
else {
block(response);
}
}
}
-(void)restyRequest:(LRRestyRequest *)request didFailWithError:(NSError *)error
{
if (errorBlock) {
if (_shouldDispatchOnMainQueue) {
dispatch_async(dispatch_get_main_queue(), ^{
errorBlock(error);
});
}
else {
errorBlock(error);
}
}
}
@end
@krstns
Copy link

krstns commented Nov 21, 2011

Awesome!

Is there any way I could control the timeout interval?

@mflint
Copy link
Author

mflint commented Nov 21, 2011 via email

@martinlong1978
Copy link

Yuk. Objective.

@mflint
Copy link
Author

mflint commented Nov 22, 2011 via email

@martinlong1978
Copy link

martinlong1978 commented Nov 22, 2011 via email

@ntoad
Copy link

ntoad commented Dec 2, 2011

In case it helps anyone else, here's a link to relevant discussion: lukeredpath/LRResty#13

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