Skip to content

bryan1anderson/GlobalActorSwiftData

Repository files navigation

@globalActor actor StorageActor: GlobalActor {

  let modelExecutor: any ModelExecutor
  private let modelContainer: ModelContainer
  var modelContext: ModelContext { modelExecutor.modelContext }

  init(modelContainer: ModelContainer) {
    self.modelExecutor = DefaultSerialModelExecutor(modelContext: ModelContext(modelContainer))
    self.modelContainer = modelContainer
  }



  static let shared = StorageActor(modelContainer: DataModel.shared.modelContainer)
}

Don't mess around with ActorModel https://stackoverflow.com/a/79651209/3786510 It's such a pain. It CAN and will run your DB code on the MainThread if you're not careful.

Instead created your own actor. And best, create a GlobalActor that allows you to decorate any struct/class/func/etc with the background isolation. This is more in line with Swift 6.2, and allows you to run all your SwiftData on a background serial thread/context.

Its great stuff. Works like a charm.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages