Skip to content

Instantly share code, notes, and snippets.

@m1entus
Last active August 29, 2015 13:57
Show Gist options
  • Save m1entus/9666239 to your computer and use it in GitHub Desktop.
Save m1entus/9666239 to your computer and use it in GitHub Desktop.
Testing asynchronous call.md
// Set the flag for a block completion handler
#define FLSTART() __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

// Set the flag to stop the loop
#define FLEND() dispatch_semaphore_signal(semaphore);

// Wait and loop until flag is set
#define FLWAIT() WAITWHILE(dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))

// Macro - Wait for condition to be NO/false in blocks and asynchronous calls
#define WAITWHILE(condition) \
do { \
while(condition) { \
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]]; \
} \
} while(0)

typedef void(^FLTestAsynchronousBlock)(void(^completion)(void));

void FLTestAsynchronous(FLTestAsynchronousBlock block) {
   FLSTART();
   block(^{
       FLEND();
   });
   FLWAIT();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment