How to handle a dependency that is using MainActor instance behind the scenes. #432
-
|
Hi. I read a few topic that I've found about tackling actors. But I dont know how to apply this thinking in my case where I used WKWebsiteDataStore.default().httpCookieStore and when that part is executed during runtime I get this error: WKWebsiteDataStore.default().httpCookieStore must be used from main thread only. Is it a good solution to just use WKWebsiteDataStore.default().httpCookieStore in each endpoint like this or is this just a cheap bypass? Here its possible because we have access to What are your thoughts on this? I will appreciate some help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @klemenzagar91, for your use case, the second code snippet you have is just fine since there is a natural singleton ( 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 And the reason this is necessary is because we want you to be able to access |
Beta Was this translation helpful? Give feedback.
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:
The
nonisolated(…