I have an Observable
that combine three other Observables
then emit a single array
. From this merged array
I would like to take the last 10 objects. But I think I take the last ten arrays
instead. Tried using compactMap
first but that didn't do the trick. It's still returned as an array
return Observable.combineLatest(breakfast, dessert, cookies) { $0 + $1 + $2 }
.compactMap { $0 }.takeLast(10)
.do(onNext: { [weak self] value in
self?.content.accept(value.compactMap {
NewRecipesCollectionViewCellViewModel(recipe: $0)})
})
With this approach you get back the elements one by one:
let breakfast = Observable.just(["egg", "milk"])
let dessert = Observable.just(["ice cream", "chocolate"])
let cookies = Observable.just(["cookie1", "cookie2"])
Observable.combineLatest(breakfast, dessert, cookies)
.map { $0 + $1 + $2 }
.do(onNext: { foods in
let lastTenFood = foods.suffix(10) // returns the last 10 elements from the array
for food in lastTenFood {
print("Food: \(food)")
}
})
.subscribe()
.disposed(by: disposeBag)