Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muukii/d4d3058b0c2f2a38a865c905ab30d56a to your computer and use it in GitHub Desktop.
Save muukii/d4d3058b0c2f2a38a865c905ab30d56a to your computer and use it in GitHub Desktop.
// 1: UIコンポーネントが持つ値と比較する
store.subscribeState { (state: State) in
if nameLabel.text != state.name {
nameLabel.text = state.name
}
if ageLabel.text != "\(state.age)" {
ageLabel.text = "\(state.age)"
}
}
// 2: 購読する機能に前回のstateを渡してもらうようにする
store.subscribeState { (state: State, oldState: State?) in
if state.name != oldState.name {
nameLabel.text = state.name
}
if state.age != oldState.age {
ageLabel.text = "\(state.age)"
}
}
// 3: Rxなどを利用してプロパティごとにストリームを生成し、重複を除外するようにする
store.rx.stateObservable()
.map(\.name)
.distinctUntilChanged()
.subscribe(onNext: { value in
nameLabel.text = value
})
store.rx.stateObservable()
.map(\.age)
.distinctUntilChanged()
.subscribe(onNext: { value in
ageLabel.text = "\(value)"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment