MVVM and RxSwift for Search Screen
<p>In purpose of education MVVM and RxSwift I want to build simple search screen, which will have a table view and a search bar. When user types something into the search bar I will show what he have in this table. Sounds pretty simple, but I can't find any tutorial which suits me.</p>

<p>I have already written all code in view controller, I just can't understand have to observe search text changes and then call database method, which will filter items by search text.</p>

<p>Some code, which I already have.</p>

<p>My ViewController</p>

<pre><code>import Foundation
import UIKit
import RxSwift
import RxCocoa

class PlaceSearchViewController: UIViewController {

    //MARK: - 

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    //MARK: - Dependencies

    private var viewModel: PlaceSearchViewModel!
    private let disposeBag = DisposeBag()

    //MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel = PlaceSearchViewModel()
        addBindsToViewModel(viewModel)
    }

    //MARK: - Rx

    private func addBindsToViewModel(viewModel: PlaceSearchViewModel) {

        searchBar.rx_text.bindTo(viewModel.searchTextObservable)

        viewModel.placesObservable.bindTo(tableView.rx_itemsWithCellFactory) {
            (tableView: UITableView, index, place: Place) in

            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PlaceCell

            cell.configureWithObject(place)

            return cell

            }
            .addDisposableTo(disposeBag)

        tableView.rx_contentOffset
            .subscribe { _ in
                if self.searchBar.isFirstResponder() {
                    _ = self.searchBar.resignFirstResponder()
                }
            }
            .addDisposableTo(disposeBag)
    }
}
</code></pre>

<p>And my view model:</p>

<pre><code>import Foundation
import RxSwift
import RxCocoa

class PlaceSearchViewModel {

    //MARK: - Dependecies
    private let disposeBag = DisposeBag()

    //MARK: - Model

    private let placesObservable: Observable&lt;[Place]&gt;
    var searchTextObservable = Variable&lt;String&gt;("")

    //MARK: - Set up

    init() {

        placesObservable = searchTextObservable.asObservable()
            //wait 0.3 s after the last value to fire a new value
            .debounce(0.3, scheduler: MainScheduler.instance)
            //only fire if the value is different than the last one
            .distinctUntilChanged()
            //convert Observable&lt;String&gt; to Observable&lt;[Place]&gt;
            .flatMapLatest { searchString -&gt; Observable&lt;[Place]&gt; in

                // some code here which I can't write.

            }
            //make sure all subscribers use the same exact subscription
            .shareReplay(1)
    }

}
</code></pre>

<p>Also, I have method <code>[DataBase searchPlaces:searchText]</code> which returns array of places - <code>[Place]</code>. I can't understand where and how place it in <code>flatMapLatest</code> of my ViewModel.</p>