How to set up Bool stream in RxSwift

I am ready to force a reboot of collectionView when new content is retrieved from the web service using RxSwift . I cannot understand why I am not getting a newContent event with the following code when my onComplete closes correctly.

 class ListingView : UIView { var newContentStream: Observable<Bool>? let disposeBag = DisposeBag() @IBOutlet weak var collectionView: UICollectionView! weak var viewModel: ListingViewModel? func bind(viewModel: ListingViewModel) { self.viewModel = viewModel } func configure() { guard let viewModel = self.viewModel else { return } self.newContentStream = viewModel.newContent.asObservable() self.newContentStream!.subscribeNext { _ in self.collectionView.reloadData() } .addDisposableTo(self.disposeBag) } } 

and then in my ViewModel:

 class ListingViewModel { let dataSource = ListingViewDataSoure() var newContent = Variable(false) func mount() { let onComplete : ([item]? -> Void) = { [weak self] items in self?.dataSource.items = items self?.newContent = Variable(true) } guard let URL = API.generateURL() else { return } Requestor.fetchAll(onComplete, fromURL: URL) } } 
+5
source share
1 answer

This is because self?.newContent = Variable(true) replaces newContent with a completely new Variable , after which you have already subscribed to the original here:

 self.newContentStream = viewModel.newContent.asObservable() self.newContentStream!.subscribeNext { ...... 

This subscription in your UIView now listens for an Observable , in which no one is going to send a Next event.

Instead, you should send the Next event to the current (and only) newContent Variable / Observable :

 self?.newContent.value = true 

You can fix this and continue to use the call to newContent Variable and reloadData , however I would not recommend doing it like this. Instead, check out RxDataSources .

+2
source

All Articles