Skip to content

What are the disadvantages of @Shared? #84

Answered by stephencelis
hmhv asked this question in Q&A
Discussion options

You must be logged in to vote

@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 be Hashable or Codable: https://swiftpackageindex.com/pointfreeco/swift-sharing/main/documentation/sharing/gotchas

And beyond …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by hmhv
Comment options

You must be logged in to vote
2 replies
@mbrandonw
Comment options

@bradhowes
Comment options

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