如何在RxSwift中将数据从委托方法传递到可观察对象的onNext方法? [英] How to pass data from delegate method to the observable's onNext method in RxSwift?

查看:139
本文介绍了如何在RxSwift中将数据从委托方法传递到可观察对象的onNext方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有管理器类,它将连接和管理蓝牙设备的数据和状态.

I have manager class which will connect and manage the data and state of the Bluetooth device.

manager类符合IWDeviceManagerDelegate并具有提供权重数据func onReceiveWeightData(_ device: IWDevice!, data: IWWeightData!)的方法.

The manager class conforms to IWDeviceManagerDelegate and has a method which gives the weight data func onReceiveWeightData(_ device: IWDevice!, data: IWWeightData!).

一旦我想从任何控制器调用listenToWeight(),我都希望使用Observable来提供数据.

Once I call listenToWeight() from any controller I want to give the data using Observable.

我如何使用onReceiveWeightData方法的数据触发onNext事件以使listenToWeight可见?

How I fire an onNext event with the data of onReceiveWeightData method to listenToWeight observable?

下面是代码.

class WeightMachineManager: NSObject {

    func setup() {
        IWDeviceManager.shared()?.delegate = self
        IWDeviceManager.shared()?.initMgr()
    }

    func listenToWeight() -> Observable<IWWeightData> {
        let tag = WeightMachineManager.tag
        if let connectedDevice = connectedDevice {
            IWDeviceManager.shared()?.add(connectedDevice, callback: { (device, code) in
                if code == .success {
                    print("\(tag)[SUCCESS] Device added successfully.")
                } else {
                    print("\(tag)[FAILURE] Failed to add device.")
                }
            })
        } else {
            print("\(tag)[FAILURE] Couldn't find any device to connect.")
        }
    }
}

extension WeightMachineManager: IWDeviceManagerDelegate {
    func onReceiveWeightData(_ device: IWDevice!, data: IWWeightData!) {
        // TODO:- Pass this data in the onNext event of listenToWeight's observable.
    }
}

推荐答案

我在下面做了很多假设,但结果应该是这样的:

I've made a lot of assumptions in the below, but the result should look something like this:

class WeightMachineManager {

    var connectedDevice: IWDevice?

    func setup() {
        IWDeviceManager.shared()?.initMgr()
    }

    func listenToWeight() -> Observable<IWWeightData> {
        if let connectedDevice = connectedDevice, let deviceManager = IWDeviceManager.shared() {
            return deviceManager.rx.add(connectedDevice)
                .flatMap { deviceManager.rx.receivedWeightData() } // maybe this should be flatMapLatest or flatMapFirst. It depends on what is calling listenToWeight() and when.
        }
        else {
            return .error(NSError.init(domain: "WeightMachineManager", code: -1, userInfo: nil))
        }
    }
}

extension IWDeviceManager: HasDelegate {
    public typealias Delegate = IWDeviceManagerDelegate
}

class IWDeviceManagerDelegateProxy
    : DelegateProxy<IWDeviceManager, IWDeviceManagerDelegate>
    , DelegateProxyType
    , IWDeviceManagerDelegate {

    init(parentObject: IWDeviceManager) {
        super.init(parentObject: parentObject, delegateProxy: IWDeviceManagerDelegateProxy.self)
    }

    public static func registerKnownImplementations() {
        self.register { IWDeviceManagerDelegateProxy(parentObject: $0) }
    }
}

extension Reactive where Base: IWDeviceManager {

    var delegate: IWDeviceManagerDelegateProxy {
        return IWDeviceManagerDelegateProxy.proxy(for: base)
    }

    func add(_ device: IWDevice) -> Observable<Void> {
        return Observable.create { observer in
            self.base.add(device, callback: { device, code in
                if code == .success {
                    observer.onNext(())
                    observer.onCompleted()
                }
                else {
                    observer.onError(NSError.init(domain: "IWDeviceManager", code: -1, userInfo: nil))
                }
            })
            return Disposables.create()
        }
    }

    func receivedWeightData() -> Observable<IWWeightData> {
        return delegate.methodInvoked(#selector(IWDeviceManagerDelegate.onReceiveWeightData(_:data:)))
            .map { $0[1] as! IWWeightData }
    }
}

这篇关于如何在RxSwift中将数据从委托方法传递到可观察对象的onNext方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆