3

Hey I'm trying to get an UICollectionView, hosted by an UICollectionViewController working with RxCocoa and RxDataSources.

Everything works fine when I use an UIViewController, with an embedded UICollectionView.

But when I try to connect via the same logic:

        self.vm.sections
        .bind(to: self.collectionView!.rx.items(dataSource: self.vm.data))
        .disposed(by: self.bag)

with an UICollectionView inside an UICollectionViewController, Xcode crashes completely.

Is there something I'm missing about RxDataSources, that you cannot use them with UICollectionViewController?

vander2675
  • 80
  • 6
  • This crashes Xcode 9.1 with a stack overflow. My guess is that when Xcode tries to show the stack trace from the crashed app, the trace is so deep that Xcode can't handle it. – Daniel T. Dec 03 '17 at 15:06

3 Answers3

5

Though I have no idea about why Xcode crashes, it seems to be caused by RxCocoa's assertion checking.

The data source of UICollectionViewController's collectionView is set by default. How about setting it to nil before binding with observable?

self.collectionView!.dataSource = nil
self.vm.sections
.bind(to: self.collectionView!.rx.items(dataSource: self.vm.data))
.disposed(by: self.bag)
Hiron
  • 1,457
  • 1
  • 13
  • 12
  • Thank you very much! That actually worked. I deleted the DataSource in IB through deleting the Connection wich didn't work. But doing the same in Code is working. – vander2675 Nov 23 '17 at 10:15
  • Thanks for that! That's solved the issue with UITableViewController as well. – oronbz Nov 28 '17 at 23:10
2

If you inherit from UITableViewController you must call tableView.datasource = nil anytime you are doing a whole table reload or refresh action

Cjay
  • 1,093
  • 6
  • 11
-1

You should not use UICollectionViewController with RxDataSource

When use RxDatasource you must use UIViewController and create a UITableView or UICollectionView inside it.

Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • Thanks. Yeah I guessed it should be that way from the Doc. But I'm experiencing right now a weird behavior with iOS 11 Large Title and an UIViewController with an UICollectionView inside – vander2675 Nov 23 '17 at 09:56
  • Hi, I would love to hear your opinion on why we shouldn't use `UICollectionViewController` or `UITableViewController` with `RxDataSources`. `UITableViewController` gives lots of good functionality that it's not so easy to implement on your own. I've found the accepted answer to solve the issue for me, and wonder what else could break by your comment. – oronbz Nov 28 '17 at 23:08
  • @oronbz A few months ago when I was developing something with RxSwift I think it was mentioned that we should not use UICollectionViewController, But as the accepted answer suggested the right solution I think my answer is wrong – Morteza Soleimani Nov 28 '17 at 23:22