在异步等待调用后更新 UI

Update UI after async await call

我从 API 加载书籍,加载时显示 activity 指示符,在服务器响应后更新标签。

activityView.isHidden = false
        
let task = detach {
    do {
        let books = try await self.bookService.fetchBooks()
        DispatchQueue.main.async {
            self.show(books: books)
        }
    } catch {
        DispatchQueue.main.async {
            self.resultLabel.text = error.localizedDescription
        }
    }
    DispatchQueue.main.async {
       self.activityView.isHidden = true
    }
}

//...

我的问题是在主队列上更新 UI 的更好方法是什么? DispatchQueue.main.async 看起来很丑,我想有更好的方法来做到这一点。

我必须使用它,因为所有 UI 更新都应该在主线程上进行,而且我在没有 DispatchQueue.main.async 的情况下遇到编译器错误

Property 'text' isolated to global actor 'MainActor' can not be mutated from a non-isolated context

Property 'isHidden' isolated to global actor 'MainActor' can not be mutated from a non-isolated context

P.S。使用 Xcode 13.0b2

像这样使用@MainActor-

self.updateAcitivityIndicator(isHidden: false)
        
let task = detach {
    do {
        let books = try await self.bookService.fetchBooks()
        self.showBooks(books)
    } catch {
        self.showError(error)
    }
    self.updateAcitivityIndicator(isHidden: true)
}

@MainActor
private func showBooks(_ books: [Book]) {
}

@MainActor
private func showError(_ error: Error) {
    self.resultLabel.text = error.localizedDescription
}

@MainActor
private func updateAcitivityIndicator(isHidden: Bool) {
    self.activityView.isHidden = isHidden
}