我想使用 BoltsSwift ( https://github.com/BoltsFramework/Bolts-Swift )。
但我无法正确处理错误。
在下面的示例中,为什么运行的是“taskHello2.continueOnSuccessWith”而不是“taskHello2.continueOnErrorWith”?
谢谢
func testTask() {
let taskHello1 = echo("hello1")
let taskHello2 = taskHello1.continueOnSuccessWith(continuation: { (task) -> Task<String> in
let taskResult = self.echo("error")
return taskResult
})
_ = taskHello2.continueOnErrorWith(continuation: { (task) -> Task<String> in
let taskResult = self.echo("Error received")
return taskResult
})
_ = taskHello2.continueOnSuccessWith(continuation: { (task) -> Task<String> in
let taskResult = self.echo("Success received")
return taskResult
})
}
func echo(_ text: String) -> Task<String> {
let taskCompletionSource = TaskCompletionSource<String>()
print("=> \(text)")
switch (text) {
case "error":
let error = NSError(domain: "domain", code: -1, userInfo: ["userInfo": "userInfo"])
taskCompletionSource.set(error: error)
case "cancel":
taskCompletionSource.cancel()
default:
taskCompletionSource.set(result: text)
}
return taskCompletionSource.task
}
输出:
=> hello1
=> error
=> Success received