Last active
December 16, 2015 08:39
-
-
Save bwoods/5407469 to your computer and use it in GitHub Desktop.
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
// From: http://samwize.com/2012/10/03/sentestingkit-does-not-support-wait-for-blocks/ | |
- (void) testBlockMethod | |
{ | |
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); | |
// Your block method eg. AFNetworking | |
NSURL *url = [NSURL URLWithString:@"http://httpbin.org/ip"]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { | |
NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]); | |
STAssertNotNil(JSON, @"JSON not loaded"); | |
// Signal that block has completed | |
dispatch_semaphore_signal(semaphore); | |
} failure:nil]; | |
[operation start]; | |
// Run loop | |
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) | |
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]]; | |
dispatch_release(semaphore); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment