kif - Objective c - Run block of code for X seconds but return immediately if condition satisfies -
so situation need run code 5 seconds if match condition want return back. doing in kif test steps , don't want block applications main thread.
sample pseudo code -
+ (bool) isverified:(nsstring*)label; { if(<condition match>) return yes; else if(x seconds not passed) <make sure m running function x seconds> else // x seconds passed now.. return no; }
if don't want block main thread in case no
should returned after 5 sec delay, structure api asynchronously.
typedef void(^ccfverificationcallbackblock)(bool verified); @interface ccfverifier : nsobject - (void)verifylabel:(nsstring *)label withcallbackblock:(ccfverificationcallbackblock)block; @end static const int64_t returndelay = 5.0 * nsec_per_sec; @implementation ccfverifier - (void)verifylabel:(nsstring *)label withcallbackblock:(ccfverificationcallbackblock)block { nsparameterassert(block); if( [label isequaltostring:@"moo"] ) block(yes); else { dispatch_time_t poptime = dispatch_time(dispatch_time_now, returndelay); dispatch_after(poptime, dispatch_get_main_queue(), ^(void){ block(no); }); } } @end
to use:
_verifier = [[ccfverifier alloc] init]; [_verifier verifylabel:@"foo" withcallbackblock:^(bool verified) { nslog(@"verification result: %d",verified); }];
Comments
Post a Comment