Created
November 21, 2011 22:01
-
-
Save mflint/1384099 to your computer and use it in GitHub Desktop.
LRResty timeout workaround
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
// | |
// 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 |
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
// | |
// 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 |
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
// | |
// 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 |
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
// | |
// 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 |
No idea I'm afraid... I'd look at the docs for NSURLConnection :)
Yuk. Objective.
No-one's forcing you to look! :-p
##
Matthew Flint - m@tthew.org
…On 21 Nov 2011, at 23:51, Martin ***@***.*** wrote:
Yuk. Objective.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1384099
I was tricked into looking.
On 22 November 2011 00:15, Matthew Flint <
reply@reply.github.com
wrote:
No-one's forcing you to look! :-p
##
Matthew Flint - ***@***.***
On 21 Nov 2011, at 23:51, Martin ***@***.*** wrote:
> Yuk. Objective.
> ---
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/1384099
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1384099
##
Martin
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
Awesome!
Is there any way I could control the timeout interval?