Search code examples
swiftuicollectionviewcellrx-swift

How to Append data in array using Behavior Relay/Subject in RxSwift?


How can i append array data in Subject type Behavior Relay RxSwift Without replace data before. I try to append data to UICollectionViewCell with binding it. It always Flashing white display, because data is replace not append but use the old data.

My code to append new data:

var photoCollectionView = BehaviorRelay<[PhotoList?]>(value: [])
let photoData = try? JSONDecoder().decode(SearchPhoto.self, from: RESPONSE_DATA)
photoCollectionView.accept(photoCollectionView.value + (photoData ?? []))

Or do you have another way to i do

Thanks for your solution :D


Solution

  • The problem isn't the source Observable, it's your Observer that needs to change.

    The default data source just does a collectionView.reloadData() every time the source emits. You need a data source that adds and removes elements instead. You can import the RxDataSources library, or you can write your own data source.