DistinctUntilChanged仅比较相等的值

问题描述

let manager = CentralManager(queue: .main)
manager.observeStateWithInitialValue()
    .filter {$0 == .poweredOn}
    .flatMap { _ in manager.scanForPeripherals(withServices: [CBUUID.myDevice.mainService])}
    .filter { $0.peripheral.identifier == someSavedIdentifier}
    .flatMap { $0.peripheral.establishConnection() }
    .flatMap { $0.discoverServices([CBUUID.myDevice.mainService]) }
    .flatMap { Observable.from($0) }
    .flatMap { $0.discoverCharacteristics([CBUUID.myDevice.businessSecret]) }
    .flatMap { Observable.from($0) }
    .flatMap { $0.observeValueUpdateAndSetNotification() }
    .distinctUntilChanged({ (old,new) -> Bool in
        if let oldValue = old.value?.toInt(),let newValue = new.value?.toInt() {
            print("old: \(oldValue),new: \(newValue)")
        }
        return old.value?.toInt() == new.value?.toInt()
    })
    .takeUntil(viewDidDisappear)
    .subscribe(onNext: { businessSecretValue in
        // business logic...
    })
    .disposed(by: disposeBag)

distinctUntilChanged用于Observable(这是蓝牙特性)。我正在尝试让序列仅包含具有独特价值的特征。但是当它们不相等时,似乎以某种方式跳过了比较。

输出样本:

old: 24,new: 24
old: 24,new: 24
old: 29,new: 29
old: 29,new: 29

这导致它永远找不到独特的元素。为什么没有一行显示:

old: 24,new: 29

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)