如何使用ReSwift调度带参数(有效负载)的异步操作

时间:2018-05-31 03:26:25

标签: ios swift reswift

我们如何使用参数调用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)

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

1 个答案:

答案 0 :(得分:0)

想出来。这是一个例子:

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)))
            }
        }
    }

}