Chapter9 about the key "nonisolated"

About nonisolated key. Still not very clear.

func store(image: UIImage, forKey key: String) async throws {
  guard let data = image.pngData() else {
    throw "Could not save image \(key)"
  let fileName = DiskStorage.fileName(for: key)
  try await storage.write(data, name: fileName)

storedImagesIndex is the actor’s variable.
Why say “It looks like this is a pure function that uses no state at all”

How to judge the function can be set a “nonisolated” ?
Maybe it means the state’s reference cant be changed right?
So invoking some function from a set or an array doesnt matter.
but setting a new set or an new array will cause the problem.

My understanding is correct?

This is the function that the “It looks like this is a pure function that uses no state at all” sentence is about:

  nonisolated static func fileName(for path: String) -> String {
    return path.dropFirst()
      .components(separatedBy: .punctuationCharacters)
      .joined(separator: "_")

You see that it accesses none of the type’s properties and therefore there is no need to run in isolation context.

How to decide if a function needs to be non-isolated? It can be non-isolated if:

  • you access only local scope variables
  • or you access only let constants from outside the local scope
  • or you are sure you never call this function asynchronously
  • or if you are sure that while you call this function asynchronously you will never write to the state that it uses (it’s possible you know that for a fact, depending on your code)