How can I wait for an async method without a callback in ObjC?

Viewed 42

I have a method of the following type:

- (void)performFoo:(int)bar;

which internally does a dispatch call onto a background thread which then enqueues an animation onto the main thread to update bar. There is no callback as shown above to capture that it is completed. I am trying to write the following code in a test:

- (void)testFoo {
  [Klass performFoo:2];

  XCTAssertTrue([Klass barValue], 2);
}

Naturally, this assert will either fail or flake since the test immediately goes to the next line since performFoo: returns immediately but has not updated the barValue. What is the best strategy that you can recommend to wait / hook till bar is finished considering that I cannot change the code for performFoo:.

Naturally, I can try [NSRunloop runUntilDate:] to just spin the runloop for some time. However, this is not optimal since it's a timed wait every time and can be too much or insufficient. Any KVO technique or so you would recommend?

0 Answers
Related