gpt4 book ai didi

swift - 如何将数据从委托(delegate)方法传递到 RxSwift 中可观察对象的 onNext 方法?

转载 作者:行者123 更新时间:2023-11-28 13:27:53 27 4
gpt4 key购买 nike

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

管理器类符合 IWDeviceManagerDelegate 并有一个提供权重数据的方法 func onReceiveWeightData(_ device: IWDevice!, data: IWWeightData!)

一旦我从任何 Controller 调用 listenToWeight(),我想使用 Observable 提供数据。

我如何使用 onReceiveWeightData 方法的数据触发 onNext 事件到 listenToWeight 可观察?

下面是代码。

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

最佳答案

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

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

关于swift - 如何将数据从委托(delegate)方法传递到 RxSwift 中可观察对象的 onNext 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com