Home IOS Development The place View.job will get its main-actor isolation from – Ole Begemann

The place View.job will get its main-actor isolation from – Ole Begemann

0
The place View.job will get its main-actor isolation from – Ole Begemann

[ad_1]

SwiftUI’s .job modifier inherits its actor context from the encircling operate. In case you name .job inside a view’s physique property, the async operation will run on the principle actor as a result of View.physique is (semi-secretly) annotated with @MainActor. Nonetheless, should you name .job from a helper property or operate that isn’t @MainActor-annotated, the async operation will run within the cooperative thread pool.

Right here’s an instance. Discover the 2 .job modifiers in physique and helperView. The code is equivalent in each, but solely one among them compiles — in helperView, the decision to a main-actor-isolated operate fails as a result of we’re not on the principle actor in that context:


Xcode showing the compiler diagnostic 'Expression is 'async' but is not marked with await'
We are able to name a main-actor-isolated operate from inside physique, however not from a helper property.
import SwiftUI

@MainActor func onMainActor() {
  print("on MainActor")
}

struct ContentView: View {
  var physique: some View {
    VStack {
      helperView
      Textual content("in physique")
        .job {
          // We are able to name a @MainActor func with out await
          onMainActor()
        }
    }
  }

  var helperView: some View {
    Textual content("in helperView")
      .job {
        // ❗️ Error: Expression is 'async' however isn't marked with 'await'
        onMainActor()
      }
  }
}

This habits is attributable to two (semi-)hidden annotations within the SwiftUI framework:

  1. The View protocol annotates its physique property with @MainActor. This transfers to all conforming varieties.

  2. View.job annotates its motion parameter with @_inheritActorContext, inflicting it to undertake the actor context from its use website.

Sadly, none of those annotations are seen within the SwiftUI documentation, making it very obscure what’s happening. The @MainActor annotation on View.physique is current in Xcode’s generated Swift interface for SwiftUI (Soar to Definition of View), however that function doesn’t work reliably for me, and as we’ll see, it doesn’t present the entire fact, both.


Xcode showing the generated interface for SwiftUI’s View protocol. The @MainActor annotation on View.body is selected.
View.physique is annotated with @MainActor in Xcode’s generated interface for SwiftUI.

To actually see the declarations the compiler sees, we have to take a look at SwiftUI’s module interface file. A module interface is sort of a header file for Swift modules. It lists the module’s public declarations and even the implementations of inlinable features. Module interfaces use regular Swift syntax and have the .swiftinterface file extension.

SwiftUI’s module interface is positioned at:

[Path to Xcode.app]/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64e-apple-ios.swiftinterface


(There will be a number of .swiftinterface recordsdata in that listing, one per CPU structure. Decide any one among them. Professional tip for viewing the file in Xcode: Editor > Syntax Coloring > Swift permits syntax highlighting.)

Inside, you’ll discover that View.physique has the @MainActor(unsafe) attribute:

@accessible(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@_typeEraser(AnyView) public protocol View {
  // …
  @SwiftUI.ViewBuilder @_Concurrency.MainActor(unsafe) var physique: Self.Physique { get }
}

And also you’ll discover this declaration for .job, together with the @_inheritActorContext attribute:

@accessible(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension SwiftUI.View {
  #if compiler(>=5.3) && $AsyncAwait && $Sendable && $InheritActorContext
    @inlinable public func job(
      precedence: _Concurrency.TaskPriority = .userInitiated,
      @_inheritActorContext _ motion: @escaping @Sendable () async -> Swift.Void
    ) -> some SwiftUI.View {
      modifier(_TaskModifier(precedence: precedence, motion: motion))
    }
  #endif
  // …
}

Xcode showing the declaration for the View.task method in the SwiftUI.swiftinterface file. The @_inheritActorContext annotation is selected.
SwiftUI’s module interface file exhibits the @_inheritActorContext annotatation on View.job.

Armed with this information, every thing makes extra sense:

  • When used inside physique, job inherits the @MainActor context from physique.
  • When used outdoors of physique, there isn’t a implicit @MainActor annotation, so job will run its operation on the cooperative thread pool by default. (Except the view accommodates an @ObservedObject or @StateObject property, which by some means makes the whole view @MainActor. However that’s a unique matter.)

The lesson: should you use helper properties or features in your view, take into account annotating them with @MainActor to get the identical semantics as physique.

By the way in which, word that the actor context solely applies to code that’s positioned straight contained in the async closure, in addition to to synchronous features the closure calls. Async features select their very own execution context, so any name to an async operate can change to a unique executor. For instance, should you name URLSession.information(from:) inside a main-actor-annotated operate, the runtime will hop to the worldwide cooperative executor to execute that technique. See SE-0338: Make clear the Execution of Non-Actor-Remoted Async Features for the exact guidelines.

I perceive Apple’s impetus to not present unofficial API or language options within the documentation lest builders get the preposterous concept to make use of these options in their very own code!

But it surely makes understanding so a lot tougher. Earlier than I noticed the annotations within the .swiftinterface file, the habits of the code firstly of this text by no means made sense to me. Hiding the main points makes issues appear to be magic once they really aren’t. And that’s not good, both.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here