用unit test测试异步线程时。有个问题,就是当主线程走完,测试就结束了,连同异步的计算或网络。不会等到异步出结果了,再来结束。所以要想办法让主线程阻塞,等下异步线程。
func wait(){
self.expectation(forNotification: "Test", object: nil, handler: nil)
self.waitForExpectations(timeout: 10, handler: nil)
}
func notify(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Test"), object: nil)
}
应用
func testExample() {
let share = FlyNetWorking.share
let filePath = "/Users/liumingqiu/Desktop/human.jpg"
share.detect(face: filePath,progress:nil, success: { (data, _) in
print("success")
print(data)
self.notify()
}) { (errorStr) in
print("errorStr:\(errorStr)\n")
self.notify()
}
wait()
}