How to stop execution of a method after a certain time in Objective-C?

How can I implement this logic in iOS (Objective-C)?

  [obj method1]

  [OCRlib imageScan] // run <10 sec. or should be stopped.
                     // Display progress bar during execution.
                     // If does not finish in 10 seconds - stop it.
                     // OCRlib - third-party code I can't change

  [obj method2]      // wait imageScan results here

      

I've seen Java / C # answers here, but not Objective-C ..

+3


source to share


1 answer


I'm not sure how to stop the method imageScan

, OCRlib

provide a method to do this?

To time out you can use dispatch groups



[obj method1]

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, queue, ^{
    [OCRlib imageScan]
});

dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW,NSEC_PER_SEC * timeout);
long status = dispatch_group_wait(group, when );

if (status != 0) {
    // block completed
    [obj method2]
}
else {
    // block failed to complete in timeout
}

      

+3


source







All Articles