Edgar
Edgar

Reputation: 223

RxSwift - Custom class as observable

I'm creating a custom generic TableView model to work with MVVM and RxSwift. I'm aware of the RxTableViewSectionedReloadDataSource class but not willing to use it now in my project.

The model is created and working

typealias TableViewModel = TableModel<CellDescriptor>

class TableModel<T> {

    var sections = [SectionModel<T>]()

    func add(item: SectionModel<T>) {
        sections.append(item)
    }

    // More funcs there...
}

I created an instance inside my ViewModel as such :

var tableViewModel = Variable<TableViewModel>(TableViewModel())

And then listen to event in my viewController

viewModel.tableViewModel.asObservable().subscribe({ [weak self] value in
    self?.tableView.reloadData()
}).addDisposableTo(dispose)

Several questions here (I'm fairly new to FRP) :

Upvotes: 2

Views: 1473

Answers (1)

solidcell
solidcell

Reputation: 7739

Don't make the view model itself an Observable. It's unnecessary complexity and it's just not intuitive. If you really wanted to, then take a look at how Variable is implemented, which should give you an idea of how to do it.

Instead, just use a subject (such as Variable) to hold onto your data within your view model.

Upvotes: 1

Related Questions