Some time ago, I wrote a factory test program and encountered a small problem. When I sent a test command, the callback function was a block and the block was processed asynchronously. Results cannot be returned in a timely manner.
After checking the information, I handled it like this.
#define TestNeedsToWaitForBlock() __block BOOL blockFinished = NO#define BlockFinished() blockFinished = YES#define WaitForBlock() while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)
-(Void) setandgetstepgoal :( INT) goal {nslog (@ "test setting motion target value: % d", goal); {testneedstowaitforblock (); [commandinterface setsteps: goal completion: ^ (bool success, nsdictionary * info) {blockfinished ();}]; waitforblock () ;}{ testneedstowaitforblock (); [commandinterface getgoal: ^ (bool success, nsdictionary * info) {int devicegoal = [info [@ "Steps"] intvalue]; nslog (@ "set motion target value result: % d >>>>>>> % d", goal, devicegoal); xctassertequal (goal, devicegoal, @ "goal does not match expectations"); blockfinished () ;}]; waitforblock ();}}
The principle is actually very simple. Use cfrunloopruninmode to stop the thread from exiting and wait for the system message.
Block of unit test