I have a question?? How to filter the state what I only want to select it to my viewmodel (viewcontroller) need to use? API and syntax in ReSwift sometimes change, so I used old syntax cause error. Please help me
From http://reswift.github.io/ReSwift/master/getting-started-guide.html:
// we are only interested in repository substate, filter it out of the overall state
store.subscribe(self) { subcription in
subcription.select { state in state.repositories }
}
Does that work for you?
Thanks for your response but it don't work for me
https://github.com/ReSwift/ReSwift/releases
Subselecting state in 4.0.0:
store.subscribe(subscriber) {
$0.select {
($0.testValue, $0.otherState?.name)
}
}
But actually the tuple doesn't work for me, only single substate like that:
store.subscribe(subscriber) {
$0.select {
$0.otherState
}
}
You should also define types in newState(state:) method
func newState(state: SomeState) {
...
}
Type of expression is ambiguous without more context
when I insert the single substate:
store.subscribe(subscriber) {
$0.select {
$0.otherState
}
}
Try to add type annotations first. Then the error will be more precise. This is probalby because subscriber's newState method parameter does not match whatever otherState is.
@DivineDominion Sorry, I do not understand
Instead of the enumerated closure parameters ($0, $1, ...), add explicit types, as in:
$0.select { (state: AppState) -> SomeSubState in
state.otherState
}
The error is Cannot invoke 'select' with an argument list of type '((AppState) -> SomeSubState)
Ok thank all, I fixed it, so I have more a question, each ViewModel always subscribe to Store and conform StoreSubscriber protocol, is there a way to write code one time??
Cool! What was the fix?
You cannot bypass implementing the subscriber protocol. You can get rid of some ceremony if you use a reactive library like RxSwift where code exists which lets you write closure subscriptions instead of dedicated objects. Thinking about it, maybe open a new "enhancement" issue where you suggest a closure-based extension without you having to write types that conform to StoreSubscriber.
The fix was simple! I have some ViewModel and a BaseViewModel, ViewModels is inheritance of BaseVM, BaseVM adopt to StoreSubscriber protocol, so my VMs only override the newState method. Now I fix it by adopting StoreSubscriber pers VM, so I can receive the State I want and select closure will be explicit
But I have a problem. I want to skip when value of state is equal, I must adopt Equatable for my State but when run app, it will be crash. The code like this:
extension EventHomeState:Equatable {
static func ==(lhs: EventHomeState, rhs: EventHomeState) -> Bool {
return lhs == rhs
}
}
Sure, this is a recursion. You need to compare the individual values of both EventHomeStates, lhs and rhs. See: http://nshipster.com/swift-comparison-protocols/
Thank guys, you are so kind. If then I have any problem, hope you also to help me.
You're welcome!