Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
Changed the way delegate responses are called.
Browse files Browse the repository at this point in the history
For regular delegates, methods will be called on whatever thread the operation executed on, which probably won't be the main thread. If delegate methods need to update the UI, they should shunt themselves onto the main thread.

For block-based callbacks, we can shunt the call on to the main thread automatically by using dispatch_async (because we don't need to wait for the callback to finish) and the main queue.

However, this causes a problem with the acceptance tests because they execute on the main thread and will lock the main thread using an NSCondition when calling assertEventually. For that reason, the option to shunt the block callback on to the main queue can be turned off.
  • Loading branch information
lukeredpath committed Jul 29, 2011
1 parent 1d1deea commit e623f4a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 68 deletions.
1 change: 1 addition & 0 deletions Classes/LRRestyClientBlockDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{
LRRestyResponseBlock block;
}
+ (void)setDispatchesOnMainQueue:(BOOL)shouldDispatchOnMainQueue;
+ (id)delegateWithBlock:(LRRestyResponseBlock)block;
- (id)initWithBlock:(LRRestyResponseBlock)theBlock;
@end
16 changes: 15 additions & 1 deletion Classes/LRRestyClientBlockDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

@implementation LRRestyClientBlockDelegate

static BOOL _shouldDispatchOnMainQueue = YES;

+ (void)setDispatchesOnMainQueue:(BOOL)shouldDispatchOnMainQueue
{
_shouldDispatchOnMainQueue = shouldDispatchOnMainQueue;
}

+ (id)delegateWithBlock:(LRRestyResponseBlock)block;
{
return [[[self alloc] initWithBlock:block] autorelease];
Expand All @@ -33,7 +40,14 @@ - (void)dealloc
- (void)restyRequest:(LRRestyRequest *)request didFinishWithResponse:(LRRestyResponse *)response
{
if (block) {
block(response);
if (_shouldDispatchOnMainQueue) {
dispatch_async(dispatch_get_main_queue(), ^{
block(response);
});
}
else {
block(response);
}
}
}

Expand Down
12 changes: 3 additions & 9 deletions Classes/LRRestyRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ - (void)start
[super start];

if ([delegate respondsToSelector:@selector(restyRequestDidStart:)]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[delegate restyRequestDidStart:self];
});
[delegate restyRequestDidStart:self];
}
[LRResty log:[NSString stringWithFormat:@"Performing %@ with headers %@", self, [URLRequest allHTTPHeaderFields]]];
}
Expand All @@ -151,9 +149,7 @@ - (void)finish;
headers:[(NSHTTPURLResponse *)self.URLResponse allHeaderFields]
originalRequest:self];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[delegate restyRequest:self didFinishWithResponse:restResponse];
});
[delegate restyRequest:self didFinishWithResponse:restResponse];

[restResponse release];

Expand All @@ -176,9 +172,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallen
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if ([delegate respondsToSelector:@selector(restyRequest:didReceiveData:)]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[delegate restyRequest:self didReceiveData:data];
});
[delegate restyRequest:self didReceiveData:data];
}
[super connection:connection didReceiveData:data];
}
Expand Down
13 changes: 0 additions & 13 deletions Tests/Acceptance/DeleteResourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ - (void)testCanPerformDeleteRequestAndPassResponseToABlock
[testLocalResponse release];
}

- (void)testResponseBlockIsCalledOnMainThread
{
__block BOOL wasCalledOnMainThread = NO;

mimicDELETE(@"/simple/resource", andReturnAnything(), ^{
[client delete:resourceWithPath(@"/simple/resource") withBlock:^(LRRestyResponse *response) {
wasCalledOnMainThread = [[NSThread currentThread] isMainThread];
}];
});

assertEventuallyWithBlock(^{ return wasCalledOnMainThread; });
}

- (void)testCanPerformSynchronousDeleteRequest
{
LRRestyResponse *response = [client delete:resourceWithPath(@"/synchronous/echo")];
Expand Down
13 changes: 0 additions & 13 deletions Tests/Acceptance/GetResourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ - (void)testCanPerformGetRequestAndPassResponseToABlock
[testLocalResponse release];
}

- (void)testResponseBlockIsCalledOnMainThread
{
__block BOOL wasCalledOnMainThread = NO;

mimicGET(@"/simple/resource", andReturnAnything(), ^{
[client get:resourceWithPath(@"/simple/resource") withBlock:^(LRRestyResponse *response) {
wasCalledOnMainThread = [[NSThread currentThread] isMainThread];
}];
});

assertEventuallyWithBlock(^{ return wasCalledOnMainThread; });
}

- (void)testCanPerformSynchronousGetRequest
{
LRRestyResponse *response = [client get:resourceWithPath(@"/synchronous/echo")];
Expand Down
16 changes: 0 additions & 16 deletions Tests/Acceptance/PostResourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,6 @@ - (void)testCanPostToResourceWithFormEncodedDataContainingNumbers
assertEventuallyThat(&receivedResponse, is(responseWithRequestEcho(@"params.number", @"123")));
}

- (void)testResponseBlockIsCalledOnMainThread
{
__block BOOL wasCalledOnMainThread = NO;

mimicPOST(@"/simple/resource", andEchoRequest(), ^{
[client post:resourceWithPath(@"/simple/resource")
payload:@"payload"
withBlock:^(LRRestyResponse *response) {

wasCalledOnMainThread = [[NSThread currentThread] isMainThread];
}];
});

assertEventuallyWithBlock(^{ return wasCalledOnMainThread; });
}

- (void)testCanPerformSynchronousPostRequest
{
LRRestyResponse *response = [client post:resourceWithPath(@"/synchronous/echo") payload:@"hello world"];
Expand Down
16 changes: 0 additions & 16 deletions Tests/Acceptance/PutResourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,6 @@ - (void)testCanPostToResourceWithFormEncodedDataContainingNumbers
assertEventuallyThat(&receivedResponse, is(responseWithRequestEcho(@"params.number", @"123")));
}

- (void)testResponseBlockIsCalledOnMainThread
{
__block BOOL wasCalledOnMainThread = NO;

mimicPUT(@"/simple/resource", andEchoRequest(), ^{
[client put:resourceWithPath(@"/simple/resource")
payload:@"payload"
withBlock:^(LRRestyResponse *response) {

wasCalledOnMainThread = [[NSThread currentThread] isMainThread];
}];
});

assertEventuallyWithBlock(^{ return wasCalledOnMainThread; });
}

- (void)testCanPerformSynchronousPutRequest
{
LRRestyResponse *response = [client put:resourceWithPath(@"/synchronous/echo") payload:@"hello world"];
Expand Down
6 changes: 6 additions & 0 deletions Tests/Acceptance/RestyClientAcceptanceTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "RestyClientAcceptanceTestCase.h"
#import "LRRestyClientBlockDelegate.h"

@implementation RestyClientAcceptanceTestCase

Expand All @@ -16,6 +17,11 @@ @implementation RestyClientAcceptanceTestCase
- (void)setUp
{
client = [LRResty newClient];

/* we can't call our callbacks on the main queue as we
have to block the main thread with assertEventually */

[LRRestyClientBlockDelegate setDispatchesOnMainQueue:NO];
}

- (void)tearDown
Expand Down

0 comments on commit e623f4a

Please sign in to comment.