Swift 6 and MainActor widgets
WidgetView is MainActor. Swift 6 language mode is the default for Flow-UI. Register widgets that are safe to render on the main actor.
Swift 6 and MainActor widgets is the isolation story for server driven UI on iOS, not a language-tour. WidgetView is @MainActor because View is. The Flow-UI package builds in the Swift 6 language mode by default. Register widgets that are safe to render on the main actor, and keep decode off the main actor the way the store already does.
Xcode 16 or newer is required. iOS 17 is the floor because Observation and the modern scroll APIs live there. There is no Swift 5 compatibility language mode in Package.swift. If your app is still on Swift 5 language mode, you can still depend on the package; you will feel the isolation at the WidgetView initializer, which is the point.
The how-to for a new type is adding a widget. The four seams are in how to build server driven UI in SwiftUI. This post is what Swift 6 changes about those seams.
Why WidgetView is MainActor#
The protocol comment is the whole rationale. Without @MainActor on WidgetView, the initializer requirement would be nonisolated while every SwiftUI conformance is main-actor bound. Swift 6 language mode rejects that mismatch.
WidgetContext.dispatch is @MainActor as well. Taps, long presses, and explicit ActionData fire on the actor that owns the view tree. ActionDispatcher is not a background queue you hop onto from a widget body.
Register at launch, on the main actor, before the first FlowPageView appears. The registry stores a @MainActor view builder. That is also the only AnyView erasure point. Your widget type stays generic until the table.
What you must not do in a widget body#
A widget body is a view. It is not a place to parse JSON, hit disk, or wait on a lock. If you need bytes, the host PageLoader already ran. If you need an image, FlowImageLoader is the seam. If you need to count, WidgetStateStore is page-scoped ephemeral state, not a database.
Do not mark your payload model @MainActor. WidgetContent is decoded off the main actor (see below). A main-actor model will fight the decode path.
Do not capture a WidgetView metatype into a @Sendable closure yourself. The registry already builds sendable thunks, including skeletons, so it does not store a metatype in a way Swift 6 rejects.
Decode is detached; views come back#
Package.swift states the split: the framework hands widget payloads to a background decode and widget views back to the main actor, so isolation is checked rather than assumed. Every target passes .swiftLanguageMode(.v6).
PageStore is @Observable and @MainActor. It owns load, refresh, pagination, and the latest DecodeDiagnostics. When bytes arrive from your PageLoader, decode runs inside Task.detached(priority: .userInitiated). The store then assigns state and diagnostics on the main actor. A slower response does not overwrite a newer one; the in-flight task is cancelled.
That is why FlowCore has no SwiftUI import. Schema types are Sendable. You can unit-test decode in a CLI or a server-side Swift test without a window.
Your PageLoader.loadPage is async throws -> Data. It can use URLSession, a file, or a fixture. Flow-UI performs no HTTP. Do not start a URLSession inside WidgetView.init.
Swift 6 language mode as the default#
FlowCore, FlowRender, FlowWidgets, and the test targets all use that array. There is no per-target opt-out in the published package. The umbrella product FlowUI is those three modules together.
What you notice as an app author:
- Your
WidgetViewmust be main-actor safe, which it is if it is ordinary SwiftUI. - Payload models should stay
Sendablevalue types:WidgetContent,Hashable, atoms from FlowCore. - Host handlers on
ActionDispatchershould be ready to run where the dispatcher runs, not assume a random executor.
If a custom widget needs a heavy compute, do it before the payload exists (backend) or in the loader, not in body. SwiftUI may evaluate body often. Isolation will not save you from that.
Register widgets that are safe to render#
"Safe" here means:
- No shared mutable global written from
body - No blocking I/O
- No assumption that
initruns once per page lifetime (SwiftUI recreates views) - State that must survive refresh lives in
WidgetStateStorekeyed by a stable widget id, not in@Statethat resets when identity changes
Stable ids are a decode concern as much as a Swift 6 concern. If the backend omits id, Flow-UI derives one from coding path so ForEach diffs. Send a real id for anything interactive.
Skeletons are @MainActor too. WidgetSkeletonProviding.skeleton() returns AnyView. Loading states mirror the widget's shape. They must be as cheap as a placeholder, because they appear while the loader is in flight.
UIKit hosts#
FlowHostingController is a UIHostingController. Push it from UIKit. The widgets are still SwiftUI and still main-actor views. You do not get a free pass to mutate UIKit from a widget body. How to implement SDUI on iOS is the iOS-shaped tutorial.
Common isolation mistakes#
The failures that show up after you flip the app to Swift 6 language mode are predictable.
Decoding in body. A let model = try! JSONDecoder().decode inside the widget view pulls work onto the main actor and hides diagnostics. Bytes belong in PageLoader and FlowCore. The view receives already-typed Content.
A main-actor payload. If OrderCardContent is annotated @MainActor because someone copied the view attribute, Task.detached decode will not compile cleanly. Keep models as Sendable structs. Views are the actor boundary.
Shared WidgetRegistry mutation from a background queue. Register at launch on the main actor. The table is locked internally, but the view builders you store are @MainActor. Do not race first paint with a late register from a network callback unless you are prepared for a frame of unknown types.
Calling UIKit from body. UIApplication.shared and view-controller lookup inside a widget will compile until you isolate the app, then it will not. Presenting is an action handler job. The widget dispatches ActionData. The host presents.
Assuming init is once. SwiftUI recreates views. Expensive work in WidgetView.init will repeat. Identity and state belong on the widget id plus WidgetStateStore, not on a stored URLSession in the view.
WidgetContext is Sendable with a @MainActor dispatch closure. Previews use WidgetContext.inert(...). Tests that render a widget without a store should use inert context rather than inventing a second dispatcher.
DIY enums versus this split#
A weekend SDUI tutorial often decodes inside a SwiftUI view and switches on an enum of widgets. That compiles in Swift 5 until it does not. Associated values, unknown types, and isolation land at the same time. The open registry keeps decode in FlowCore and view construction in a main-actor closure created in the same generic register call, so the internal cast cannot fail.
You can still roll your own. You will rebuild WidgetRegistry, Task.detached decode, and @MainActor WidgetView. That comparison is a later architecture post. For this package, the isolation is already decided.
Read next#
- Adding a widget is the three-step recipe: model, view, register.
- How to build server driven UI in SwiftUI names the four seams.
- How to implement SDUI on iOS covers UIKit hosting and unknown types.
Swift 6 and MainActor widgets: WidgetView is @MainActor, the package language mode is v6, decode is detached, register views that only paint. The server still sends JSON. The main actor still owns the pixels.
Related documentation
Related articles
- Production6 min read
Decode diagnostics in production
Surface DecodeDiagnostics in debug overlays and logs. A malformed widget should be a telemetry event, not a blank screen.
- Production6 min read
Accessibility in server driven SwiftUI
Native widgets inherit VoiceOver. The schema still has to send alt text, button traits, and type that scales.
- Production6 min read
Theming server driven SwiftUI
Hex colors with dark_hex out of the box. Design tokens through ThemeProvider. Light and dark without a second payload.
More on the blog.
Read the source
Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.
GitHubGet started with Flow-UI
Install the Swift package, register a widget, and render a page from JSON.
Get started