How to make parts of an actor nonisolated 如何使 actor 的各个部分非隔离

How to make parts of an actor nonisolated 如何使 actor 的各个部分非隔离

温馨提示:本文最后更新于2025-02-08 17:50:48,某些文章具有时效性,若有错误或已失效,请在下方留言

All methods and mutable properties inside an actor are isolated to that actor by default, which means they cannot be accessed directly from code that’s external to the actor. Access to constant properties is automatically allowed because they are inherently safe from race conditions, but if you want you can make some methods excepted by using the nonisolated keyword.
默认情况下,Actor 内的所有方法和可变属性都与该 Actor 隔离,这意味着不能直接从 Actor 外部的代码访问它们。自动允许对常量属性的访问,因为它们本质上不受争用条件的影响,但如果需要,可以使用 nonisolated 关键字使某些方法例外。

Actor methods that are non-isolated can access other non-isolated state, such as constant properties or other methods that are marked non-isolated. However, they cannot directly access isolated state like an isolated actor method would; they need to use await instead.
非隔离的 Actor 方法可以访问其他非隔离状态,例如常量属性或标记为非隔离的其他方法。但是,它们不能像独立 actor 方法那样直接访问独立状态;他们需要改用 await

To demonstrate non-isolated methods, we could write a User actor that has three properties: two constant strings for their username and password, and a variable Boolean to track whether they are online. Because password is constant, we could write a non-isolated method that returns the hash of that password using CryptoKit, like this:
为了演示非隔离的方法,我们可以编写一个具有三个属性的 User actor:两个用于其用户名和密码的常量字符串,以及一个用于跟踪它们是否在线的变量 Boolean。因为 password 是恒定的,所以我们可以编写一个非隔离的方法,使用 CryptoKit 返回该密码的哈希值,如下所示:

import CryptoKit
import Foundation

actor User {
    let username: String
    let password: String
    var isOnline = false

    init(username: String, password: String) {
        self.username = username
        self.password = password
    }

    nonisolated func passwordHash() -> String {
        let passwordData = Data(password.utf8)
        let hash = SHA256.hash(data: passwordData)
        return hash.compactMap { String(format: "%02x", $0) }.joined()
    }
}

let user = User(username: "twostraws", password: "s3kr1t")
print(user.passwordHash())
下载图标
how-to-make-parts-of-an-actor-nonisolated-1.zip
zip文件
50.2K

I’d like to pick out a handful of things in that code:
我想从该代码中挑选出一些内容:

  1. Marking passwordHash() as nonisolated means that we can call it externally without using await.
    将 passwordHash() 标记为nonisolated意味着我们可以在外部调用它而无需使用 await
  2. We can also use nonisolated with computed properties, which in the previous example would have made nonisolated var passwordHash: String. Stored properties may not be non-isolated.
    我们还可以将nonisolated属性与计算属性一起使用,在前面的示例中,这将使 nonisolated var passwordHash: String .存储的属性不能是非隔离的。
  3. Non-isolated properties and methods can access only other non-isolated properties and methods, which in our case is a constant property. Swift will not let you ignore this rule.
    非隔离属性和方法只能访问其他非隔离属性和方法,在我们的例子中是常量属性。Swift 不会让您忽略此规则。

Non-isolated methods won’t help when it comes to protocol conformance – things like Codable and Equatable are strictly synchronous only at the time of writing.
在协议一致性方面,非隔离方法无济于事——像 Codable 和 Equatable 这样的东西只有在编写时才严格同步。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享