简体   繁体   English

将 ReSwift 与子状态一起使用时,如何避免在更新另一个子状态时收到不需要的子状态更新?

[英]When using ReSwift with substates, how can I avoid receiving unwanted substate updates, when another substate is updated?

When using ReSwift with substates, how can i avoid receiving unwanted substate update ( SubstateA ), when updating a another substate (SubstateB)当使用ReSwift与子状态,如何避免接收到不想要子状态更新(SubstateA),更新另一个子状态(SubstateB)时

I though that was the whole point haven substates...我虽然这是重点避风港子状态...

State.swift状态.swift

struct MainState: StateType {
    var subStateA: SubstateA?
    var subStateB: SubstateB?
}

struct SubstateA: StateType {
    let value: String
}

struct SubstateB: StateType {
    let value: String
}

Store.swift商店.swift

let mainStore = ReSwift.Store<MainState>(reducer: { action, state -> MainState in

    var newState = state ?? MainState()

    switch action {

    case let anAction as UpdateSubstateA:
        newState.subStateA = newState.subStateA ?? SubstateA(value: anAction.value)

    case let anAction as UpdateSubstateB:
        newState.subStateB = newState.subStateB ?? SubstateB(value: anAction.value)

    default:
        break
    }
    return newState
}, state: nil)

Actions.swift Actions.swift

struct UpdateSubstateA: Action {
    let value:String
}

struct UpdateSubstateB: Action {
    let value:String
}

ViewController.swift视图控制器.swift

class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {

        mainStore.subscribe(self)  { $0.select { state in (state.subStateB ) } }

        mainStore.dispatch(UpdateSubstateA(value: "a"))
        mainStore.dispatch(UpdateSubstateB(value: "b"))
    }
}

extension ViewController: StoreSubscriber {

    func newState(state: SubstateB?) {
        print("SubstateB updated")
    }

    typealias StoreSubscriberStateType = SubstateB?
}

Although I dispatched a single update action for SubstateB I also receive newState events when SubstateA is updated虽然我为 SubstateB 调度了一个更新操作,但当 SubstateA 更新时,我也会收到 newState 事件

Console安慰

SubstateB updated
SubstateB updated
SubstateB updated

Question is old, but maybe an answer will be useful for somebody.问题很老,但也许答案对某人有用。

The subscription could be:订阅可以是:

mainStore.subscribe(self)  { $0.select { $0.subStateB }.skipRepeats(==) }

SubstateB should conform to the Equatable protocol: SubstateB 应该符合 Equatable 协议:

extension SubstateB: Equatable {
    static func == (lhs: Self, rhs: Self) -> Bool {
        return lhs.value == rhs.value
    }
}

Console:安慰:

SubstateB updated - nil (initial value)
SubstateB updated - value: "b"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中更新应用程序时如何避免数据丢失? - how i can avoid my data lost when i updates my apps in iOS? 为什么当我的基础数据源更新时 SwiftUI 会自动向后导航,如何避免这种行为? - Why does SwiftUI automatically navigate backwards when my underlying data source is updated and how can I avoid this behavior? 将NSStrings转换为NSDates时如何避免语言问题? - How can I avoid language issues when converting NSStrings to NSDates? 使用XCTest点击UI元素时,如何避免NSInternalInconsistencyException? - How can I avoid NSInternalInconsistencyException when tapping on UI elements with XCTest? 当管理对象模型更新时,其关系模型不会更新 - When a managed object model updates its relationship model is not getting updated 连接确实完成接收数据后,如何更改ios视图? - How can I change ios view when connection did finish receiving the data? 如何将对象与通知存储在一起,以便在收到通知时可以调用它? - How to store an object together with a notification so that I can call it when receiving the notification? 当视图可见时,如何正确订阅位置更新? - How do I propertly subscribe to location updates when a View is visible? 如何使用 ReSwift 组织上传/下载方法的动作状态流 - How organize action-state flow of upload/download methods using ReSwift 即使应用程序处于后台或终止状态,我该如何采取措施接收远程通知? - How can I do an action on receiving a remote notification even when the app is in background or terminated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM