What’s the best way to sleep/wait until certain notification is posted?
I’ve got class with asynchronous API that gets data from NSNotification (notification comes from Cocoa, I can’t avoid it) and I need to unit test it.
So far I’ve figured out semi-busy wait:
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]]
without processing of run loop notifications aren’t received. But how can I break from run loop immediately when desired notification comes?
runMode:beforeDate: will run the loop just once, so you can use that call in a loop until you detect that the notification has been handled (if nothing else, your handler can flip a member variable in your test class to YES).