I have a function in ViewModel which is getting some data from network file as Single. In viewModel I am using map to convert it to different model and return it to ViewController. Once this map/conversion is complete I want to update a BehaviorRelay object in ViewModel to tell its subscribers that downloading is complete. I am not able to update this BehaviorRelay object.
I tried to add some code in the function but I get error in return statement.
var showLoading = BehaviorRelay<Bool>(value: true)
func getPropertyList(city cityID: String) -> Single<[Property]> {
return propertyListApi.getPropertyList(city: cityID).map({ [weak self] propertInfo -> [Property] in
propertInfo.map({
return Property(name: $0.name, type: "Property Type: " + $0.type, lowestPricePerNight: "", overallRatingPercentage: "Rating: " + String($0.overallRating.overall ?? 0), image: self?.getImageURL(images: $0.images))
})
})
}
I want to update showLoading in getPropertyList function to let ViewController know that loading is complete.
You can do this by subscribing your showLoading
to the result... Also note that showLoading
should be a let, not a var.
let showLoading = BehaviorRelay<Bool>(value: true)
func getPropertyList(city cityID: String) -> Single<[Property]> {
let result = propertyListApi.getPropertyList(city: cityID).map({ [weak self] propertyInfo -> [Property] in
propertyInfo.map({
return Property(name: $0.name, type: "Property Type: " + $0.type, lowestPricePerNight: "", overallRatingPercentage: "Rating: " + String($0.overallRating.overall ?? 0), image: self?.getImageURL(images: $0.images))
})
})
result
.asObservable() // convert your Single to something that can emit multiple values.
.map { _ in false } // when the request emits a value, emit `false`.
.startWith(true) // when the request starts, emit `true`.
.catchErrorJustReturn(false) // if the request fails, emit `false`.
.bind(to: showLoading) // listen to the emissions above and set yourself accordingly.
.disposed(by: disposeBag) // if self is deleted, cancel the subscription.
return result
}