0

I have a worker thread that I keep alive through a loop that's controlled by a flag. I need the thread to stay alive for the length of my application as it opens a permanent connection to a remote server.

I fire up that thread and call several methods on it with:

[worker performSelector:@selector(getBusy) onThread:worker withObject:nil waitUntilDone:NO];

This seems to work fine and the method is called. At some point in getBusy I try to call a method in the main thread with:

[delegate performSelectorOnMainThread:@selector(gotBusy) withObject:nil waitUntilDone:NO

where delegate is a reference to the class that starts the separate thread.

The problem is that gotBusy never gets called on the main thread. I've peppered it with NSLog() statements and I can't see them printed on the console.

What should I be looking for to debug this?

ruipacheco
  • 15,025
  • 19
  • 82
  • 138

2 Answers2

2

First, make sure delegate is not nil. Secondly, make sure your main event loop is not blocked and not running in a modal mode.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Yes, the main even loop was sleeping. Your comment made a lightbulb go on in my head. – ruipacheco Apr 15 '13 at 15:14
  • Excellent -- note that you should never block or sleep the main thread. You should also avoid scheduling lots of fine grained events (like, say, a timer that goes off every 1/10th of a second to poll for data). – bbum Apr 15 '13 at 16:54
  • This is a unit test. I had the sleep being called while a flag was set to false and didn't realise that would block the background thread from being able to call the main thread. – ruipacheco Apr 15 '13 at 20:55
0

Is it possible that you never assigned delegate, so you're calling performSelectorOnMainThread on a nil object? You could try setting waitUntilDone to YES so your worker thread will block and let the delegate do its work.