简体   繁体   English

如何使用 ReSwift 组织上传/下载方法的动作状态流

[英]How organize action-state flow of upload/download methods using ReSwift

I'm building application using Redux architecture with ReSwift framework, most of applications processes described by action - reducer - state flow.我正在使用 Redux 架构和 ReSwift 框架构建应用程序,大多数应用程序流程由操作 - 减速器 - 状态流描述。 Now I have added two methods upload(image: UIImage, fileName: String) and download(fileName: String), which uploading and downloading images to Firebase storage.现在我添加了两个方法 upload(image: UIImage, fileName: String) 和 download(fileName: String),它们将图像上传和下载到 Firebase 存储。 So, how I can describe theses methods using Redux flow?那么,我如何使用 Redux 流程来描述这些方法呢? Well, I know how I can run downloading/uploading processes using middleware, but how I can get results of download/upload in store subscribers?好吧,我知道如何使用中间件运行下载/上传过程,但是如何在商店订阅者中获得下载/上传的结果?

You can create actions that will either hold the results or an error like this:您可以创建将保存结果或错误的操作,如下所示:

struct ImageDownloadStartAction: Action {
    let filename: String
}

struct ImageDownloadSuccessAction: Action {
    let data: Data
}

struct ImageDownloadFailureAction: Action {
    let error: Error
}


struct ImageUploadStartAction: Action {
    let image: UIImage
    let filename: String
}

struct ImageUploadSuccessAction: Action { }

struct ImageUploadFailureAction: Action {
    let error: Error
}

Then in your completionHandler or done block, you can use the dispatch function passed to the middleware to dispatch the success/failure action on the main thread (for example, DispatchQueue.main.async ) and use the value or error in your reducer.然后在您的completionHandlerdone块中,您可以使用传递给中间件的dispatch函数在主线程(例如, DispatchQueue.main.async )上调度成功/失败操作,并在您的减速器中使用值或错误。

For example (using PromiseKit ):例如(使用PromiseKit ):

let networkMiddleware: Middleware<AppState> = { dispatch, getState in
    return { next in
        return { action in
            next(action)

            switch action {
            case let action as ImageDownloadStartAction:
                networkService.download(filename: action.filename)
                .done { // automatically called on main thread in PromiseKit
                    dispatch(ImageDownloadSuccessAction(data: $0)
                }
                .catch { // automatically called on main thread in PromiseKit
                    dispatch(ImageDownloadFailureAction(error: $0)
                }

            default:
                break
            }
        }
    }

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

相关问题 如何使用ReSwift调度带有参数(有效负载)的异步动作 - How to dispatch an async action with parameters(payload) with ReSwift ReSwift - 如何处理依赖于旧状态的状态更改以及视图中的新状态 - ReSwift - How to deal with state changes that depend on old state as well as new state in the View 如何组织后台批量下载? - How to organize background bulk download? 将 ReSwift 与子状态一起使用时,如何避免在更新另一个子状态时收到不需要的子状态更新? - When using ReSwift with substates, how can I avoid receiving unwanted substate updates, when another substate is updated? 如何使用ASIHTTPRequest跟踪上传/下载进度 - How to Using ASIHTTPRequest to tracking Upload/Download progress 如何使用Xcode从/向Dropbox上传/下载文件 - How to upload/download a file from/to dropbox using Xcode 如何在ReSwift中定义带有多个子选择的`newState`? - How to define `newState` in ReSwift with multiple sub-selects? 如何在iphone(ios)上的服务器上传和下载数据 - How to upload and download data on server in iphone(ios) 在为iPhone和iPad开发通用应用程序时如何组织变量和方法定义 - How to organize the variable and methods definitions when developing universal application for iPhone and iPad 如何在Xamarin iOS中将文件上传和下载到iCloud - How to Upload and download files to iCloud in xamarin ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM