david
david

Reputation: 3583

Tell dispatch_main_queue to work up all its blocks from main queue?

I am doing a test with AFHTTPClient to test the backend response.

__block id testedResponseObject = nil;
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    testedResponseObject = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    testedResponseObject = nil;
}];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(testedResponseObject, @"");

The problem with this is that it waits for all operations to finish but it does not execute the success block because it gets scheduled to dispatch_get_main_queue(). Is there a way to tell dispatch_get_main_queue() to finish its blocks from the main queue?

Upvotes: 0

Views: 125

Answers (1)

mattt
mattt

Reputation: 19544

Instead of relying on completionBlock, you can access the responseData (or whatever property) directly:

NSURLRequest *request = [client requestWithMethod:@"GET" path:path parameters:nil];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[client enqueuHTTPRequestOperation:operation];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(operation.responseData, @"");

Upvotes: 1

Related Questions