无法在SwiftUI中更新Redux流的全局状态

时间:2020-09-07 20:40:48

标签: swift swiftui

我正在尝试在SwiftUI中实现Redux状态,并且在执行动作创建者之后一直坚持更新全局状态。我不知道在商店调度功能中该做什么。

// Store.swift 

typealias ActionCreator = (_ dispatch: @escaping (Action) -> ()) -> ()

func getMovies() -> ActionCreator {
    return { dispatch in
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            dispatch(.populateMovies([Movie(title: "ABC")]))
        }
    
    }
}

class Store: ObservableObject {
    
    var reducer: Reducer
    @Published private (set) var appState: AppState
    
    init(appState: AppState, reducer: Reducer) {
        self.appState = appState
        self.reducer = reducer
    }
    
    func dispatch(_ dispatch: ActionCreator) {
        // how to send the movies to the reducer to update the state 
        //self.reducer.update(&appState, dispatch)
    }
}



// ContentView: 

store.dispatch(getMovies()) 

0 个答案:

没有答案