Skip to content

ViewStores' state out of sync #909

Answered by mbrandonw
rurza asked this question in Q&A
Discussion options

You must be logged in to vote

In general it is not recommended to access the view store's state from inside a .sink on its publisher. This is because the publisher emits on will change rather than did change, and so the view store's state may differ from what is handed to the .sink closure. Note that this is not a design choice TCA makes, but rather how @Published fields and SwiftUI work.

So, rather than doing:

viewStore.publisher
  .sink { _ in 
    self.update()
  }

func update() {
  viewStore.someField
}

You should pass around the view store's emission explicitly:

viewStore.publisher
  .sink { state in 
    self.update(state: state)
  }

func update(state: State) {
  state.someField
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rurza
Comment options

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