Swift Swfit学习Swift

【iOS】swift - PromiseKit的应用

2019-05-10  本文已影响97人  梦蕊dream

一、事件总线

事件总线是对发布和订阅设计模式的一种实现,通过发布、订阅可以将组件间一对一和一对多的耦合关系解开。这种设计模式,特别适合数据层通过异步发布数据的方式告知 UI 层订阅者,使得 UI 层和数据层可以不用耦合在一起,在重构数据层或者 UI 层时不影响业务层。

1.1 Delegate实现

通过 Delegate 回调给 UI 层来进行展示,但是这个只适合一对一的模式。如果异步处理完后,还需要将数据发布给其他 UI 进行处理和展示的话,就需要继续发布给其他 Delegate,从而造成 Delegate 套 Delegate 的情况。

1.2 Block 实现

Block 和使用 Delegate 的情况类似。如果需要不断异步发布给下一个数据订阅者的话,也会出现 Block 回调嵌套其他 Block 回调的情况

1.3 KVO & NSNotificationCenter

使用 KVO 是强依赖属性的,只要更新了属性就会发布给所有的观察者,对应关系过于灵活,难以管控和维护。NSNotificationCenter 也有类似的问题,通过字符串来维护发布者和订阅者之间的关系,不仅可读性差,而且和 KVO 一样面临着难以管控和维护的情况。

由于 Delegate 和 Block 只适合做一对一数据传递,KVO 和 NSNotificationCenter 虽然可以支持一对多的数据传递,但存在过于灵活而无法管控和维护的问题,而事件总线需要通过发布和订阅这种可管控方式实现一对一和一对多数据传递。由此可以看出,iOS 现有的 Delegate、Block、KVO、NSNotificationCenter 等技术并不适合来做事件总线。

二、Promise

Promise 对象保存异步数据操作,同时 Promise 对象提供统一的异步数据操作事件处理的接口。这样,事件总线的数据订阅和数据发布事件,就可以通过 Promise 对象提供的接口实现出来,比以前通过 Delegate 回调处理异步事件来说更加合理。

2.1 Promise 对象状态
2.2 Promise 对象重要的方法

Promise 对象每次执行完 then 和 catch 方法后,这两个方法会返回先前的 Promise 对象,同时根据异步操作结果改变 Promise 对象的状态。整个异步发布和订阅操作都以同步操作的方式表现出来了。Promise 对象不仅能够避免回调层层嵌套,而且通过的统一接口,使得事件总线的发布和订阅操作更加规范和易用。

三、PromiseKit

GitHub可查看源码和相关文档,这里就简单描述下如何使用。

3.1 PromiseKit的使用
firstly {
    // 异步获取当前用户信息
    fetchUserInfo()
}.then { userInfo in
    // 使用异步获取到的用户信息中的 uid 再去异步获取用户的 timeline
    fetchUserTimeline(uid: userInfo.uid)
}.then { timeline in
    // 记录 timeline
    self.timeline = timeline
}.catch {
    // 整个方法链的错误都会在这处理
}

firstly {
    when(fulfilled: fetchImage, fetchLocation)
}.done { image, location in
    self.imageView.image = image
    self.label.text = "\(location)"
}.ensure {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}.catch { error in
    self.show(UIAlertController(for: error), sender: self)
}
3.2 其他方法
3.3 实战

导入框架

Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'test' do
    pod 'PromiseKit'
end

主要应用于网络请求的回调
1.空返回值的示例

//MARK: - 网络请求方法,用于传必要参数和获取数据【返回值为空】
func getList() ->Promise<Void>{
    self.toast?.showLoadingDlg()
    return Promise(resolver: { (resolver) in
        let success = {(result: Any?) -> () in
            //网络请求成功 do something
            resolver.fulfill_()
        }
        let failure = {(result: Any?) -> () in
            //网络请求成功 do something
            resolver.reject(NetworkError.NoData)
        }
        //封装的网络请求工具类
        Network().postWithPath(path: API_GET_LIST, type: nil, responseType: "json", paras: nil, success: success, failure: failure)
    })
}

//方法的调用
_ = getList().done({ () in
    //成功时,更新UI or 刷新页面 do something
}).catch({ (Error) in
    //异常时 do something
})

2.有返回值的示例

//MARK: - 网络请求方法,用于传必要参数和获取数据【返回值根据所需设置】
func getMsg(id: Int) -> Promise<Dictionary<String,Any>> {
    return Promise(resolver: { (resolver) in
        let success = {(result: Any?) -> () in
            let dict = JSON(result!).dictionaryObject
            //返回为Dictionary类型得对象
            resolver.fulfill(dict!)
        }
        let failure = {(result: Any?) -> () in
            //失败后的error信息
            resolver.reject(NetworkError.NoData)
        }
        let paras = [
            "id": id,
            ] as [String : Int]
        Network().postWithPath(path: API_MSG, type: nil, responseType: "json", paras: paras, success: success, failure: failure)
    })
}

//方法调用
_ = self.getMsg(id: ID).done({ (dict) in
    print("=========dict:\(dict)")
    //使用Dictionary的回调, 更新UI or 刷新数据 do someing
}).catch({ (err) in
    //输入网络请求异常的提示语 dos something
})
上一篇 下一篇

猜你喜欢

热点阅读