【问题标题】:How to dispatch an async action with parameters(payload) with ReSwift如何使用 ReSwift 调度带有参数(有效负载)的异步操作
【发布时间】:2018-05-31 03:26:25
【问题描述】:

我们如何使用带参数的 ReSwift 调度异步操作?

我已经创建了这个异步操作:

func searchItems(state: AppState, store: Store<AppState>) -> Action? {
    var items = [Artwork]()
    HttpHelper.post(url: serverApi.search.rawValue, params: ["term": ""]) { data in
        if let data = data as? [[String: Any]] {
            for row in data {
                items.append(Artwork(data: row))
            }
            DispatchQueue.main.async {
                store.dispatch(UpdateItems(items: items))
            }
        }
    }
    return nil
}

为了调度一个动作,我使用了

store.dispatch(searchItems)

但我不知道如何在操作中附加搜索词。

【问题讨论】:

    标签: ios swift reswift


    【解决方案1】:

    想通了。这是一个例子:

    import ReSwift
    
    struct GetShowroom: Action {
    
        let code: String
    
        init(code: String) {
            self.code = code
            getIt()
        }
    
        func getIt() {
            store.dispatch(UpdateLoading(loading: .Showroom(code: self.code)))
            HttpHelper.post(to: .getShowroom, params: ["code": self.code]) { res in
                if let data = res as? [String: Any] {
                    store.dispatch(UpdateShowroom(showroom: Showroom(code: self.code, data: data)))
                }
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多