2

I have written a script similar to below code. I kept breakpoint inside the "dispatch_async", but it is not getting executed. What could be the issue here?

 dispatch_async(dispatch_get_main_queue()) {

    let array = response!.allObjects as! [NSArray]    
   XCTAssertNotEqual(array.count, 0, testPassed);
   self.waitForExpectationsWithTimeout(10, handler: { (error: NSError?) -> Void in
       if(error != nil) {
          XCTFail("Failed with error: \(error)
      }
     })


 }
Coder
  • 1,661
  • 4
  • 27
  • 50

1 Answers1

2

This dispatch_async can't make sense in a unit test. The unit test is running on the main queue. It won't release the queue to run another block until the unit test finishes, at which point it is too late to run this block.

As written, the only way this makes sense is to remove the dispatch_async entirely. You're on the main thread. Just execute the code you want.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks Rob for the quick reply. When I remove dispatch_async, the unit test script is crashing. I have asked a question in "https://stackoverflow.com/questions/33576790/xctest-crashing-in-xctestexpectations-in-ios". Not sure what is causing the crash... If I execute a single test function, it is working fine. But if I run test scripts for entire project (nearly 1000 tests), it is crashing. – Coder Nov 09 '15 at 02:29