How to wrap an asynchronous class to make it synchronous? Use NSRunLoop? How to wrap an asynchronous class to make it synchronous? Use NSRunLoop? multithreading multithreading

How to wrap an asynchronous class to make it synchronous? Use NSRunLoop?


You want a semaphore, which will allow your primary codepath to block until your asynchronous callback signals the semaphore and allows it to continue.

Semaphores are available in iOS 4 through Grand Central Dispatch.

It appears that the behavior of semaphores can be implemented in iOS 3 with NSCondition.


I just implemented something similar. The context was: - wrap several calls to ZipArchive in a background thread - for each call to unzip, display a different progress meter (infinitely spinning wheel with the name of the file being expanded) - perform a cleanup task when all the archives have been expanded

Turns out NSConditionLock makes it straightforward, and the code works as follows:

NSConditionLock* lock = alloc/initfor(int idx = 0; idx < [list count]; idx++) {  NSString* fileName = ["getIdx's element in list"]  [cond lockWhenCondition:idx]; //  ... prepare things before forking  [cond unlockWithCondition:-1];  [Notification forkBlock:^(void){    [cond lockWhenCondition:-1];    NSAutoreleasePool *pool = [alloc/init];    [ZipArchive unzipFile:fileName];    [pool release];    [cond unlockWithCondition:idx+1];  }];}[cond lockWhenCondition:[list count];// all the tasks are now completed

Each of the blocks are scheduled in a background thread. The Notification class takes care of animating the UIView with the spinning wheel and wrapping the block in another thread. Lock/unlock must be called from the same thread, so the condition is what enables the ping-pong between from and background threads (-1 for the background, 1,2,3..n for the foreground).


Ok, so there are a few different issues here, I'm trying to think where to start.

But just so we understand what you're trying to accomplish, when you say you want the call to "appear" synchronous, do you mean you want the call to block? Are you making this call from the main thread? If so, then it seems you are blocking the main thread by design.

Keep in mind the third party library is probably scheduling events on the main run loop. You can create your own run loop and run it in another thread, but have you told the other library to use that run loop for its events? (For example, it could be making async network requests that are scheduled on the main run loop which you have blocked)

I would rethink what you're doing a bit, but first we would need to know if your intention is to block the thread from which you are making this call. Also, do you need to support iPhoneOS 3.x?