What are the disadvantages of @Shared? #84
-
I feel that @shared is very powerful, but are there any disadvantages? If there are no major disadvantages, I think that from now on, almost all of TCA Reducer's State can be replaced with @shared(.inMemory..), what do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@hmhv There are a few things that stand out:
And beyond all that, the bottom line is So in general we would not recommend using |
Beta Was this translation helpful? Give feedback.
-
In terms of the @SharedReader with GRDB, my testing shows an interesting "feature" of having @SharedReader in reducer state: the shared queries will be executed if that reducer is pushed onto a Also, I had mistakenly "over shared" some state attributes and these attributes affected the GRDB query. This led to strange |
Beta Was this translation helpful? Give feedback.
@hmhv There are a few things that stand out:
Mutating a shared value is less ergonomic (
state.$property.withLock { $0 = newValue }
vs.state.property = newValue
) and also less efficient: a lock to this global, mutable state is required to provide exclusive access and avoid data races.@Shared
observation is not minimal, unlike@Observable
classes and@ObservableState
in a TCA reducer. So you will see inefficient observation when you apply@Shared
to large value types.There are also gotchas listed in the following article that note how
@Shared
values can't beHashable
orCodable
: https://swiftpackageindex.com/pointfreeco/swift-sharing/main/documentation/sharing/gotchasAnd beyond …