Last active
November 11, 2020 06:49
-
-
Save muukii/d4d3058b0c2f2a38a865c905ab30d56a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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