After filtering an Observable list, I might have an empty list. I'm only interested in events that contain a populated list. Is there any way to stop empty events propagating to onNext
?
let source: BehaviorRelay<[Int]> = .init(value: [])
source
.map { nums -> [Int] in
return nums.filter { $0 < 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)
CodePudding user response:
What about using .filter
? You could do this:
.map { nums -> [Int] in
return nums.filter { $0 < 10 }
}
.filter { !$0.isEmpty }
Or you could also validate that whenever you get an event like this:
.subscribe(onNext: { nums in
guard !nums.isEmpty else { return }
print("OKPDX \(nums)")
})