Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wrap an asynchronous class to make it synchronous? Use NSRunLoop?
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>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.</p> <p>To simulate a join I did the following. I added a NSConditionalLock to NFCThread:</p> <pre><code>joinLock = [[NSConditionLock alloc] initWithCondition:NO]; </code></pre> <p>The code around the call to Connection looks something like this:</p> <pre><code>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]; </code></pre> <p>The protocol for the delegate has three methods. In NFCThread I implemented each method something like this:</p> <pre><code>- (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; } </code></pre> <p>I overloaded NFCThread's main method so that it just loops continually. Something like this:</p> <pre><code>while (!callBackInvoked) { ; } </code></pre> <p>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:</p> <pre><code>NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; while (!callBackInvoked) { [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } </code></pre> <p>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.</p> <p>I feel like I'm missing something obvious here. Any help much appreciated.</p> <p>Rich</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload