View Model Initialization

Home / Uncategorized / View Model Initialization

In most of the examples I see using MVVM & RxSwift, the View Models have a lot code in the initializer, rather than modularizing it into helper methods. The only reason why this seems to make sense is to prevent optional observable properties. Should I do the same, so I don’t need to unwrap optional outputs when binding and so forth?

For example, in one of my View models I have an optional output to bind to a collection view named bids.
var bids: Observable<[Bid]>?

its optional because the value is assigned in a method, but in most open source code I see, this is all done in init. which is best approach?
private func fetchBids() {

let fetchedBids = FetchBidsTask(itemId: dispatcher)
.filter { !$0.isCompleted }

bids = fetchedBids.filter { $0.element != nil }.dematerialize()
.map { $0.sorted(by: { (bid1, bid2) -> Bool in
return bid1.maxBid > bid2.maxBid })
.do(onNext: {
self.maxBid = $0[0]
self.tableViewDataChanged = Variable(true)

error = fetchedBids.filter { $0.error != nil }.map { $0.error! }


Im just now realizing the language is a bit confusing. The object Bid has a property named maxBid which is the dollar amount of the bid. the viewModel also has a property named maxBid which is the highest bid out of all the bids that were fetched, so when time runs out, I know who the highest bidder is.


Read more

Leave a Reply

Your email address will not be published. Required fields are marked *