我正在尝试新的 async/await 东西。我的目标是test()在后台运行该方法,所以我使用asyncDetached; 但是在test()我需要在主线程上进行调用时,所以我使用了 MainActor。
(我意识到这可能看起来很复杂,但它是从一个更好的现实世界案例中缩减而来的。)
好的,所以测试代码看起来像这样(在视图控制器中):
override func viewDidLoad() {
super.viewDidLoad()
asyncDetached(priority: .userInitiated) {
await self.test()
}
}
@MainActor func getBounds() async -> CGRect {
let bounds = self.view.bounds
return bounds
}
func test() async {
print("test 1", Thread.isMainThread) // false
let bounds = await self.getBounds()
print("test 2", Thread.isMainThread) // true
}
Run Code Online (Sandbox Code Playgroud)
第一个print说我不在主线程上。这就是我所期望的。
但是,第二个print说,我是在主线程上。那不是我所期望的。
感觉好像我神秘地回到主线程只是因为我调用了一个 MainActor 函数。我以为我会等待主线程,然后在我已经在的后台线程中恢复。
这是一个错误,还是我的期望错了?如果是后者,怎么做我时走出主线程await,但再回来线程我是吗?我认为这正是 async/await 可以简化的事情......?
(在某种程度上,我可以通过在调用 …
我希望能够让该函数doSomething()不class B存在async并且不阻止它的调用者线程。但使用以下代码我收到此错误:
无法将“() async -> Void”类型的函数传递给需要同步函数类型的参数
并且 xcode 尝试强制该doSomething()函数class B成为async
class A {
func doSomething() async throws {
//perform
}
}
class B {
let a = A()
func doSomething() {
DispatchQueue.global().async {
do {
await try a.doSomething()
} catch {
print(error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在一个简单的 Mac 测试项目中有以下代码:
\n@main\nclass AppDelegate: NSObject, NSApplicationDelegate {\n func applicationDidFinishLaunching(_ aNotification: Notification) {\n print("are we initially on the main thread: \\(Thread.isMainThread)")\n\n Task {\n print("is new task on main thread: \\(Thread.isMainThread)")\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在Swift 语言指南中,我看到以下内容:
\n\n\n要创建在当前参与者上运行的非结构化任务,请调用 Task.init(priority:operation:) 初始值设定项。要创建不属于当前参与者的非结构化任务(更具体地称为分离任务),请调用 Task.detached(priority:operation:) 类方法。
\n
因此,由于 AppDelegate 代码在主要参与者上运行,并且我正在创建一个正常(即非分离)任务,因此我希望它的子任务也在主要参与者上运行。
\n但当我运行这个测试应用程序时,情况并非如此:
\nare we initially on the main thread: true\nis new task on main thread: false\nRun Code Online (Sandbox Code Playgroud)\n根据我所读到的有关 Swift 并发性的内容,我预计新任务将在主要参与者上调度并运行,因此Thread.isMainThread在该任务中也是如此。
为什么事实并非如此?
\n