Swift BrightFuture 的所有回调都发生在主线程上,对吗?

huangapple go评论63阅读模式
英文:

All Swift BrightFuture callbacks happen on the main thread, right?

问题

当我们使用BrightFutures进行异步请求时,相应的...代码块会在哪个线程上运行?

我的理解是它们将始终在main线程上运行,对吗?

英文:

When we make an asynchronous request using BrightFutures.

service.makeSomeRequest(
    param1: value1,
    param2: value2
)
.onSuccess { [weak self] result in
    ...
}
.onFailure { [weak self] error in
    ...
}

Which threading context will the respective ... blocks run in?

My understanding is that they will always be on main thread, right?

答案1

得分: 0

不要紧,找到这篇有用的文章,记录了预期的行为:

  • 如果该方法从主线程调用,该块将在主队列上执行

  • 如果该方法不是从主线程调用,该块将在全局队列上执行

所以基本上,.onSuccess(...).onFailure(...) 回调的执行上下文取决于调用 service.makeSomeRequest(...) 时的原始上下文。

英文:

Nevermind, found this helpful article documenting the expected behavior:

  • If the method is called from the main thread, the block is executed on the main queue

  • If the method is not called from the main thread, the block is executed on a global queue

So basically, the context in which the .onSuccess(...) and .onFailure(...) callbacks will be executed depends on the original context in which the service.makeSomeRequest(...) was called.

huangapple
  • 本文由 发表于 2023年3月4日 01:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定