BehaviourRelay variable remove duplicates

1.4k views Asked by At

Just inherited this code base and I'm not quite familiar with RxSwift. I've done some reading but cannot find a solution to my problem.

We have a variable declared. This saves information from a network call, and then is being used to load information to a collection view. Therefore the collection view is showing multiple similar items

var allPhotos = BehaviorRelay<[Photo]>.init(value: [])

How do I convert it to a set to remove duplicates?

2

There are 2 answers

0
hell0friend On BEST ANSWER

Try this:

let filteredPhotos = allPhotos.asObservable().map { Array(Set($0)) }

PS: Make sure Photo conforms to Hashable

0
Sateesh Yemireddi On

You can use RxSwiftExt's distinct operator to eliminate duplication in any observable. Infact, it is a good library for different extensions. Install it through Cocoa Pods or Carthage.

var filteredPhotos = allPhotos.asObservable().distinct().toArray()