RxSwift - block/stop/ignore 条件满足时的事件

RxSwift - block/stop/ignore event if condition met

过滤 Observable 列表后,我可能得到一个空列表。我只对包含填充列表的事件感兴趣。有什么方法可以阻止空事件传播到 onNext?


        let source: BehaviorRelay<[Int]> = .init(value: [])

        source
            .map { nums -> [Int] in
                return nums.filter { [=11=] < 10 }
            }
            /// What can go here to stop/block/ignore an empty list
            .subscribe(onNext: { nums in
               print("OKPDX \(nums)")
            })

        source.accept([1, 9, 13])
        // prints "[1, 9]" (all good)

        source.accept([20, 22, 101])
        // prints "[]" (not desirable, I'd rather not know)

使用 .filter 怎么样?你可以这样做:

.map { nums -> [Int] in
        return nums.filter { [=10=] < 10 }
     }
.filter { ![=10=].isEmpty }

或者您也可以在收到这样的事件时进行验证:

.subscribe(onNext: { nums in
      guard !nums.isEmpty else { return }
      print("OKPDX \(nums)")
})