Will Bolam
Will Bolam

Reputation: 309

iOS - Is is possible to pause execution, allow the main loop to run, then resume execution?

Is is possible in an iOS app to do the following:

I've searched and can't find anything that allows me to do this, but I have a feeling I've seen it done in the past by a programmer I previously worked with.

The motivation for this is the following:

There are 2 ways I can think to resolve this:

  1. Split my test up and use performSelector:withObject:afterDelay:
  2. Specify YES for waitUntilDone when sending the message

Both of these solutions are ok, but 1. complicates the test quite a lot, and 2. changes the messaging system I'm writing tests for, so will have to be carefully considered.

Considering what performSelectorOnMainThread:withObject:waitUntilDone:YES does, it seems like the functionality I'm asking for should be possible (as it's similar in many ways), but is it possible?

Upvotes: 0

Views: 518

Answers (2)

Jeff Loughlin
Jeff Loughlin

Reputation: 4174

This might be what you're looking for:

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];

This will execute one iteration of the main run loop and then continue on from there. This is kind of abusing the way the run loop is supposed to work though, so you might want to consider a different design - it's almost never a good idea to pause the main thread. Maybe do what you need to do in a separate thread and have that thread call back to a delegate method when it's done, or use a notification.

Upvotes: 4

macrene
macrene

Reputation: 314

Use multithreading for this purpose. SENDER on the mainThread, WATCHER on a secondThread, what allows you to start WATCHING before SENDING.

Upvotes: 0

Related Questions