alexxjk alexxjk - 3 months ago 32x
Swift Question

RxSwift: Append elements to Observable<[_]>

I have an array (

) that is a datasource for a tableview. I want to be able to append new elements to it and update the tableview every time new elements are appended to the array. I can't find how to add new elements to


Use a Subject such as Variable. Then just treat the value property as your Array and append to it to add new elements. Subscribe to the Variable via asObservable().

I've simplified the code example by using String, however you'll want to use some kind of UITableViewCell.

let dataSource = Variable<[String]>([])

dataSource.value.append("some string A")

    .subscribeNext { e in

dataSource.value.append("some string B")

Once you have your dataSource, you'll want to hook it up to a tableView via

dataSource.asObservable().bindTo(yourTableView.rx_itemsWithCellIdentifier("MyCellClass", cellType: MyCellClass.self)) { (row, element, cell) in
      // do your cell configuration here