How to properly use custom executor on global actor?

Thanks @vns and @ktoso

I edited @vns's snippet to actually retain the executor, otherwise I got a crash.

@globalActor
actor MyGlobalActor: GlobalActor {
  static let shared = MyGlobalActor()
  private static let executor = GlobalActorSerialExecutor()
  static let sharedUnownedExecutor: UnownedSerialExecutor = MyGlobalActor.executor
    .asUnownedSerialExecutor()

  nonisolated var unownedExecutor: UnownedSerialExecutor {
    Self.sharedUnownedExecutor
  }
}

It now works.
Thank s a lot for your help, finding info and samples on how to do this is difficult, I tried for 2 hours :)

6 Likes