捕捉“自我”在`@Sendable`闭包中
我的应用程序使用以下带有 IBAction
的视图控制器。
当某个按钮触发该操作时,该按钮将被禁用,并启动一些后台处理。
当后台处理完成后,按钮将再次启用,这必须在主线程上完成。
final class DebugViewController: UIViewController {
button.isEnabled = false
// …
@IBAction func action(_ sender: Any) {
// …
Task {
// some background processing
DispatchQueue.main.async {
self.button.isEnabled = true
}
}
// …
}
}
在构建期间,语句 self.button.isEnabled = true
中显示以下警告:
Capture of 'self' with non-sendable type 'DebugViewController' in a `@Sendable` closure
我可以通过将 DebugViewController 声明为来避免此警告但是
final class DebugViewController: UIViewController, @unchecked Sendable {
,当我分析而不是构建代码时,我得到现在这一行出现以下错误:
Redundant conformance of 'DebugViewController' to protocol 'Sendable'
因此我有点困惑:
- 每个
UIViewController
Sendable
是否如“冗余一致性”错误所示? - 如果是这样,为什么我会收到 DebugViewController 不可发送的警告?
- 如果 UIViewController 不可发送,为什么我会收到“冗余一致性”错误?
最终,如何正确执行此操作?
My app uses the following view controller with an IBAction
.
When the action is triggered by some button, the button is disabled, and some background processing is started.
When background processing is finished, the button is enabled again, which has to be done on the main thread.
final class DebugViewController: UIViewController {
button.isEnabled = false
// …
@IBAction func action(_ sender: Any) {
// …
Task {
// some background processing
DispatchQueue.main.async {
self.button.isEnabled = true
}
}
// …
}
}
During a build, the following warning is shown at the statement self.button.isEnabled = true
:
Capture of 'self' with non-sendable type 'DebugViewController' in a `@Sendable` closure
I could avoid this warning by declaring the DebugViewController as
final class DebugViewController: UIViewController, @unchecked Sendable {
However, when I analyze instead of build the code, I get now the following error at this line:
Redundant conformance of 'DebugViewController' to protocol 'Sendable'
Thus I am a little confused:
- Is every
UIViewController
Sendable
, as the "Redundant conformance" error suggests? - If so, why do I get the warning that DebugViewController is not Sendable?
- If a UIViewController is not Sendable, why do I get then a Redundant conformance" error?
And eventually, how to do it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这显然是 Xcode 13.3 Beta 2 中的一个错误。在发布的版本 13.3 (13E113) 中,没有显示任何警告。
This was apparently a bug in Xcode 13.3 Beta 2. In the released Version 13.3 (13E113), no warning is shown.