Skip to content
Discussion options

You must be logged in to vote

Hi @klemenzagar91, for your use case, the second code snippet you have is just fine since there is a natural singleton (default()) to use.

In the situation where you do not have a singleton and you want an instance, you really have no choice but to create a separate endpoint on your dependency for creating the main actor object, and then you have to invoke that endpoint early on in your app's lifecycle:

extension MyDependency: DependencyKey {
  static var liveValue: Self {
    nonisolated(unsafe) var store: SomeMainActor!
    return Self(
      init: { store = await SomeMainActor() },
      fetch: { await store.fetch() },
      delete: { await store.delete() }
    )
  }
}

The nonisolated(…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@klemenzagar91
Comment options

Answer selected by klemenzagar91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants