What’s the best way to make a synchronous version of an asynchronous method in Objective-C (for iOS, in case it matters)?
Say you have a class with these two methods:
- (void) asyncDoSomething; // Starts an asynchronous task. Can't be changed.
- (void) onFinishDoSomething; // Called when the task is finished
How would you implement a synchronous doSomething that does not return until the task is finished?
Ok, so you can declare a global boolean that will tell you to continue or wait, just before calling your
- (void)asyncDoSomething;method you set yourBOOL waittoYESand after the method you doto wait for your response, and in you async callback
- (void)onFinishDoSomething;you set your boolean toNO;This way, your method is still called asynchronous but the code after is not executed before the response. It will wait like if it was synchronous.