RxSwiftCommunity / RxDataSources

UITableView and UICollectionView Data Sources for RxSwift (sections, animated updates, editing ...)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clarity on init with `Self` requirement of SectionModelType in RxDataSources

lukya opened this issue · comments

I posted this on StackOverflow first, but I assume, this community should be able to help me better / faster in this case.

Binding a sectioned table with RxDataSources using a TableViewSectionedDataSource, requires sections which conform to SectionModelType.

This SectionModelType protocol has the following initializer as one of it's requirements:

    init(original: Self, items: [Item])

In addition, the same protocol enforces var items: [Item] { get }. We can now initialize the items array backing variable (in above mentioned init) with either from original.items, or items passed as init parameter. This is very confusing. The SectionModelType code has no comments.

How section of README which explains creation of sections for this very case, talks about creating typealias (for associated value), and the items array, but not a word about following implementation of init with original: Self :

init(original: SectionOfCustomData, items: [Item]) {
    self = original
    self.items = items
}

While this can work in a struct, doing the same in a class shouts:

Cannot assign to value: 'self' is immutable

Can anyone here explain what is happening here and why do we NEED to init with Self

Lastly, is there another (cleaner) way to reactively (in RXSwift / RxCocoa only) bind a sectioned table view to an observable datasource.
e.g.

  • my cells, and sections have their own data model, which need to be mutable (hence classes)
  • there are multiple screens with this requirement for different entities, so i would be interesetd in achieving this with protocols instead, and slap the corresponding data model with the protocol ans have a common implementation for RXBinding

Any pointers to either get clarity more on existing implementation, or achieving above points would be really helpful.

I have this problem now. I need to add a new mutating field in my SectionModel.
@lukya Do you have a workaround?