grace
grace

Reputation: 73

BehaviourRelay variable remove duplicates

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?

Upvotes: 0

Views: 1584

Answers (2)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4409

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()

Upvotes: 2

hell0friend
hell0friend

Reputation: 569

Try this:

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

PS: Make sure Photo conforms to Hashable

Upvotes: 0

Related Questions