Using [NSQperueQueue mainQueue] in XCTests

I am testing part of my code using XCTest, which also adds NSOperations to the main queue. It looks like this:

[NSOperationQueue mainQueue] addOperationAsBlock:^{ // some code happens here }];

The code starts when the application starts on the device or in the simulator, but it does not start at all when the unit test starts (I can not get to the debug point in the first line of the block).

call:

[NSOperationQueue mainQueue] waitUntilAllOperationsAreFinished];

doesn't help either.

Any suggestions? I think that I do not have enough code to initialize the queue.

* EDIT *

Thanks for your answers, I added my final code for completeness:

 // add as many operations as you'd like to the mainQueue here __block BOOL continueCondition = YES; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // this should be the last operation continueCondition = NO; }]; while (continueCondition) { [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } // continue your test here 

This works because mainQueue is guaranteed to be non-competitive, so the last operation that was added will be the last - so you don’t even have to change your code to stop the test cycle.

+6
source share

Source: https://habr.com/ru/post/956494/


All Articles