Swift GlobalActors & Dependencies #36
-
I know the topic of working with Swift Actors was mentioned at some point during the TCA video series. I'm curious to know if there are any reasons not to mix Dependency with a GlobalActor type. The GlobalActor type enhances the Singleton pattern we have. A dependency can have a mutable state. I did read the docs about ActorIsolated Type in this library. After implementing the globalActor to one of my Dependency wrapper, it's making me think maybe another need the Dependency var |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Having a global actor would help dependencies isolated on the same actor to interdepend synchronously. As I can imagine this happening in user code bases, I'm not sure there is something the library can do to help in this area, or maybe I'm not understanding your suggestion correctly. You can of course isolate parts of your dependencies to global actors, and this is "commonly" performed with If this is not a singleton, I wouldn't recommend to implement your dependency as an actor itself, global or not. If you're not cautious, this reference-type will leak through dependencies overrides, and you may find bugs caused by action at distance that are difficult to fix. Whereas you're making this singleton a global actor is up to you, but I'm not exactly seeing how the library could help here, beside providing helpers to handle this object synchronously (if this is even possible in the general picture). Values that you extract from Do you have an example where the global actor is causing friction, or where the situation could be improved? |
Beta Was this translation helpful? Give feedback.
Having a global actor would help dependencies isolated on the same actor to interdepend synchronously. As I can imagine this happening in user code bases, I'm not sure there is something the library can do to help in this area, or maybe I'm not understanding your suggestion correctly.
You can of course isolate parts of your dependencies to global actors, and this is "commonly" performed with
MainActor
. It is also possible to completely isolate dependencies, but you'll need to markliveValue
& co asnonisolated
to conform toDependencyKey
(if you're usingSelf
asValue
), and you'll need to do a little gymnastic to synchronously create values of your isolated dependency to conform to the pr…