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
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?