Skip to content

Using State in effect calculation can lead to crashes with @ObservableState #2919

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

You must be logged in to vote

I think this behavior is understandable, and it is technically reproducible in vanilla SwiftUI, but because classes aren't trivially sendable it typically comes with warnings.

For now it can be addressed in an app by:

  1. Avoiding capturing the entirety of state in an effect. Instead pluck out the fields that are needed.
  2. Mark the effect @MainActor. Even if an expensive computation is performed in an effect, the local effect's context can usually be @MainActor and not tie things up.

We're open to other ideas, though. If anyone thinks that we can leverage concurrency and its diagnostics better, please let us know!

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@KaiOelfke
Comment options

@mbrandonw
Comment options

@KaiOelfke
Comment options

@mbrandonw
Comment options

Answer selected by KaiOelfke
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