Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm currently working on an iPhone app and I have a library from a third-party that has asynchronous behavior but that I'd like to wrap with my own class and make it appear synchronous.

The central class in this library, let's call it the Connection class, has several functions that have their ultimate result resolved when methods on an instance of a delegate class are called. What I'm trying to do is wrap this class and delegate so that it appears to be synchronous instead of asynchronous. If I were doing this in Java I would use FutureTask or a CountdownLatch or just join(). But I'm not sure the best way to do this in Objective C.

I started by creating a NSThread extenstion, NFCThread, which conforms to the above mentioned delegate protocol. The idea is that I would init and NFCThread, pass the NFCThread instance to Connection's setDelegate method, start the thread and then call an asynchronous method on Connection. My expectation is that one of the three delegate methods on the NFCThread instance would be called ultimately causing the thread to exit.

To simulate a join I did the following. I added a NSConditionalLock to NFCThread:

joinLock = [[NSConditionLock alloc] initWithCondition:NO];

The code around the call to Connection looks something like this:

NFCThread *t = [[NFCThread alloc] init];
[connection setDelegate:t];
[t start];

[connection openSession];
// Process errors, etc...

[t.joinLock lockWhenCondition:YES];
[t.joinLock unlock];
[t release];
[connection setDelegate:nil];

The protocol for the delegate has three methods. In NFCThread I implemented each method something like this:

- (void)didReceiveMessage:(CommandType)cmdType 
                     data:(NSString *)responseData 
               length:(NSInteger)length {
    NSLog(@"didReceiveMessage");
    // Do something with data and cmdType...
    [joinLock lock];
    [joinLock unlockWithCondition:YES];
    callBackInvoked = YES;
}

I overloaded NFCThread's main method so that it just loops continually. Something like this:

while (!callBackInvoked) { ; }

I found that this isn't really a good idea since it cause cpu usage to go through the roof. So instead I tried using a run loop from some examples I found on this site:

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

while (!callBackInvoked) {
    [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

In both of my implementations the main thread is always blocked and it appears that none of the delegate methods are ever called. However, I know that the library is functioning properly and that calls to the delegate methods are normally called.

I feel like I'm missing something obvious here. Any help much appreciated.

Rich

like image 886
richever Avatar asked Aug 05 '10 22:08

richever


1 Answers

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.

like image 66
Seamus Campbell Avatar answered Sep 24 '22 16:09

Seamus Campbell