I have following code:
let categoriesRequest = APIService.MappableRequest(Category.self, resource: .categories)
let categoriesRequestResult = api.subscribeArrayRequest(categoriesRequest, from: actionRequest, disposedBy: disposeBag)
let newCategories = categoriesRequestResult
.map { $0.element }
.filterNil()
let categoriesUpdateData = DatabaseService.UpdateData(newObjectsObservable: newCategories)
let categoriesDatabaseResult = database.subscribeUpdates(from: categoriesUpdateData, disposedBy: disposeBag)
let latestTopicsRequest = APIService.MappableRequest(Topic.self, resource: .latestTopics)
let latestTopicsRequestResult = api.subscribeArrayRequest(latestTopicsRequest, from: actionRequest, disposedBy: disposeBag)
let newLastTopics = latestTopicsRequestResult
.map { $0.element }
.filterNil()
let latestTopicsUpdateData = DatabaseService.UpdateData(newObjectsObservable: newLastTopics)
let latestTopicsDatabaseResult = database.subscribeUpdates(from: latestTopicsUpdateData, disposedBy: disposeBag)
There are two requests that starts from the same publish subject actionRequest, and two database updates after these requests.
I need something like isActive, bool value which will return true if any of api/database task is in progress. Is it possible? I saw ActivityIndicator in RxSwift examples, but I don't know is it possible to use it in my case.
Code from api/database if needed:
// API
func subscribeArrayRequest<T>(_ request: MappableRequest<T>,
from triggerObservable: Observable<Void>,
disposedBy disposeBag: DisposeBag) -> Observable<Event<[T]>> {
let result = ReplaySubject<Event<[T]>>.create(bufferSize: 1)
triggerObservable
.flatMapLatest {
SessionManager
.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
.mapResponse(on: APIService.mappingSheduler) { Mapper<T>().mapArray(JSONArray: $0) }
.materialize()
}
.subscribe(onNext: { [weak result] event in
result?.onNext(event)
})
.disposed(by: disposeBag)
return result
}
// Database
func subscribeUpdates<N, P>(from data: UpdateData<N, P>, disposedBy disposeBag: DisposeBag) -> Observable<Void> {
let result = PublishSubject<Void>()
data.newObjectsObservable
.observeOn(DatabaseService.writingSheduler)
.subscribe(onNext: { [weak result] newObjects in
// update db
DispatchQueue.main.async {
result?.onNext(())
}
})
.disposed(by: disposeBag)
return result
}
Thanks.
Found following solution: pass ActivityIndicator instance to services methods and use it like this:
func bindArrayRequest<T>(_ request: MappableRequest<T>,
from triggerObservable: Observable<Void>,
trackedBy indicator: RxActivityIndicator,
disposedBy disposeBag: DisposeBag) -> Observable<Event<[T]>> {
let result = ReplaySubject<Event<[T]>>.create(bufferSize: 1)
triggerObservable
.flatMapLatest {
SessionManager
.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
.mapResponse(on: APIService.mappingSheduler) { Mapper<T>().mapArray(JSONArray: $0) }
.materialize()
.trackActivity(indicator)
}
.bind(to: result)
.disposed(by: disposeBag)
return result
}
Also wrap database update logic into observable and use it with .trackActivity(indicator) into flatMapLatest like in api.
Then use api/database methods like this:
let indicator = RxActivityIndicator()
isUpdatingData = indicator.asObservable() // needed bool observable
let categoriesRequest = APIService.MappableRequest(Category.self, resource: .categories)
let categoriesRequestResult =
api.bindArrayRequest(categoriesRequest,
from: actionRequest,
trackedBy: indicator,
disposedBy: disposeBag)
let newCategories = categoriesRequestResult
.map { $0.element }
.filterNil()
let categoriesUpdateData = DatabaseService.UpdateData(newObjectsObservable: newCategories)
let categoriesDatabaseResult =
database.bindUpdates(from: categoriesUpdateData,
trackedBy: indicator,
disposedBy: disposeBag)
let latestTopicsRequest = APIService.MappableRequest(Topic.self, resource: .latestTopics)
let latestTopicsRequestResult =
api.bindArrayRequest(latestTopicsRequest,
from: actionRequest,
trackedBy: indicator,
disposedBy: disposeBag)
let newLastTopics = latestTopicsRequestResult
.map { $0.element }
.filterNil()
let latestTopicsUpdateData = DatabaseService.UpdateData(newObjectsObservable: newLastTopics)
let latestTopicsDatabaseResult =
database.bindUpdates(from: latestTopicsUpdateData,
trackedBy: indicator,
disposedBy: disposeBag)
isUpdatingData observable is what I needed.
Related
I am completely new to RxSwift. I managed to load my table view but now I would like to make a call every 10 seconds.
I was reading here that I should probably use Observable<Int>.interval(10, scheduler: MainScheduler.instance), I tried without much success.
class MarketService: MarketServiceProtocol {
func fetchMarkets() -> Observable <[Market]> {
return Observable.create { observer -> Disposable in
RestManager.shared.makeRequest(withEndPoint: "market/v2/get-summary?region=US" , withHttpMethod: .get) { result in
if let error = result.error {
observer.onError(error)
return
}
guard let response = result.response,
200 ... 299 ~= response.httpStatusCode else {
return
}
guard let data = result.data else {
return
}
do {
let decodedData = try JSONDecoder().decode(MarketResult.self, from: data)
observer.onNext(decodedData.marketSummaryAndSparkResponse.markets)
} catch {
observer.onError(error)
}
}
return Disposables.create { }
}
}
}
then I call in my view controller:
viewModel.fetchMarketViewModels().observe(on: MainScheduler.instance).bind(to: tableView.rx.items(cellIdentifier: HomeTableViewCell.cellIdentifier)) {
index, viewModel, cell in
guard let cell = cell as? HomeTableViewCell else { return }
cell.setupData(viewModel: viewModel)
}.disposed(by: self.disposableBag)
There are a couple of problems with your Observable.create closure. You have to make sure that something is sent to the observer in every path, otherwise the Observable will call the function and then not emit anything and you will not know why.
Also, you want to minimize the amount of logic being performed in the closure passed to create. You are doing way too much in there.
So let's simplify the code in the create closure as much as possible first:
extension RestManager {
func rx_makeRequest(withEndPoint endPoint: String, withHttpMethod method: HttpMethod) -> Observable<(response: MyHTTPURLResponse, data: Data)> {
Observable.create { observer in
self.makeRequest(withEndPoint: endPoint, withHttpMethod: method) { result in
if let response = result.response, let data = result.data {
observer.onNext((response, data))
observer.onCompleted()
}
else {
observer.onError(result.error ?? RxError.unknown)
}
}
return Disposables.create() // is there some way of canceling a request? If so, it should be done here.
}
}
}
This does the bare minimum. Just wraps the underlying callback and nothing else. Now your fetchMarkets call is much simpler:
class MarketService: MarketServiceProtocol {
func fetchMarkets() -> Observable <[Market]> {
return RestManager.shared.rx_makeRequest(withEndPoint: "market/v2/get-summary?region=US", withHttpMethod: .get)
.do(onNext: { result in
guard 200...299 ~= result.response.httpStatusCode
else { throw URLError.httpRequestFailed(response: result.response, data: result.data) }
})
.map { try JSONDecoder().decode(MarketResult.self, from: $0.data).marketSummaryAndSparkResponse.markets }
}
}
Now to the meat of your question. How to make the network call every 10 seconds... Just wrap your network call in a flatMap like this:
Observable<Int>.interval(.seconds(10), scheduler: MainScheduler.instance)
.flatMapLatest { _ in
viewModel.fetchMarketViewModels()
}
.observe(on: MainScheduler.instance)
.bind(to: tableView.rx.items(cellIdentifier: HomeTableViewCell.cellIdentifier)) { index, viewModel, cell in
guard let cell = cell as? HomeTableViewCell else { return }
cell.setupData(viewModel: viewModel)
}
.disposed(by: self.disposableBag)
Learn more about flatMap and its variants from this article.
I want to show the contents of the collection view through search. However, there are many errors while binding collection views. I think the model code got weird first, where is it weird? How can we fix this error?
output.loadData.map(CollectionOfOne.init).bind(to: collectionView.rx.items) { (row,collectionView, element) -> UICollectionViewCell in
let indexPath = IndexPath(index: 1)
guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "friendCell", for: indexPath) as? FriendCell else {
return FriendCell()
}
cell.profileImage?.image = UIImage(systemName: element.image)
cell.nickNameLbl?.text = element.nickname
cell.listenBtn?.rx.tap.subscribe(onNext: {[unowned self] in
selectIndexPath.accept(row)
}).disposed(by: cell.disposeBag)
return cell
}
output.searchNameResult.map(CollectionOfOne.init).bind(to: collectionView.rx.items) { collectionView, index, element -> UICollectionViewCell in
let indexPath = IndexPath(index: 0)
guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "freindCell", for: indexPath) as? FriendCell else {
return FriendCell()
}
// cell.profileImage?.image = UIImage(systemName: (element as AnyObject).image)
cell.profileImage?.image = UIImage(named: element.img)
cell.nickNameLbl?.text = (element.nickname)
return cell
}
This is viewcontroller code
struct input {
let loadData: Signal<Void>
let searchName: Driver<Void>
let searchHashTag: Driver<Void>
}
struct output {
let loadData: BehaviorRelay<[User]>
let searchNameResult: PublishRelay<users>
}
func transform(_ input: input) -> output {
let api = SearchAPI()
let loadData = BehaviorRelay<[User]>(value: [])
let searchName = PublishRelay<users>()
input.loadData.asObservable().subscribe(onNext: { [weak self] in
guard let self = self else { return }
api.getFriend().subscribe(onNext: { (response, statuscode) in
switch statuscode {
case .ok:
// loadData.accept(response!, User)
result.onCompleted()
default:
result.onNext("default")
}
}).disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
input.searchName.asObservable().withLatestFrom(input.name).subscribe(onNext: {[weak self] nickname in
guard let self = self else { return }
api.getUserList(nickname).subscribe(onNext: { (response,statuscode) in
switch statuscode {
case .ok:
result.onCompleted()
case .noHere:
result.onNext("no user")
default:
result.onNext("default")
}
}).disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
And this is my viewmodel.
What is the problem?
You are getting the error: Incorrect argument label in call (have 'named:', expected 'systemName:') because of this other error: Value of type 'users' has no member 'img'.
I expect that you don't really want to display a single cell but the code is so muddled in general that it's hard to make out exactly what you are trying to do. I suggest you find yourself a tutor who can teach you about types, or possibly use the Swift Playgrounds app to learn the basics of how to code before you try to make a real program.
I have this code:
...
let minMaxReq = networkProvider.getMinMaxAmortization(id: id)
let emitExtractReq = networkProvider.getEmitExtract(id: id)
self.isLoading.accept(true)
Observable.zip(minMaxReq.asObservable(), emitExtractReq.asObservable()) { (minMaxResp, emitExtractResp) in
return (minMaxResp, emitExtractResp)
}.subscribe(onNext: { [weak self] responses in
let minMaxResp = responses.0
let emitExtractResp = responses.1
guard let self = self else { return }
self.isLoading.accept(false)
self.getMinMaxAmortizationResponse.accept(minMaxResp)
self.receiptsCNH.accept(emitExtractResp)
}, onError: { [weak self] error in
self?.isLoading.accept(false)
self?.receivedError.accept(error)
}).disposed(by: disposeBag)
In this case all errors from both requests will end up in the onError closure, how can I handle the error from minMaxReq in a different onError closure?
My goal is to make the 2 requests at the same time but handle their error with different closures. How can I achieve this?
thanks
Have you tried the materialize operator? Maybe it can be useful for the implementation you need. Annex an example of how it is usually used:
let valid = network.getToken(apikey)
.flatMap{ token in self.verifyToken(token).materialize()}
.share()
valid
.compactMap { $0.element }
.subscribe(onNext: { data in print("token is valid:", data) })
.disposed(by: disposeBag)
valid
.compactMap { $0.error?.localizedDescription }
.subscribe(onNext: { data in print("token is not valid:", data) })
.disposed(by: disposeBag)
In that way, you could divide the stream into two and give it the appropriate treatment.
Another option might be to manipulate the error event in the minMaxReq operation. Something similar to:
let minMaxReq = networkProvider.getMinMaxAmortization(id: id)
.catchError({ error in Observable.empty()
.do(onCompleted: { /* Do anything with no side effect*/ }) })
let emitExtractReq = networkProvider.getEmitExtract(id: id)
Observable.zip(...)
Here is an article that explains more detail Handling Errors in RxSwift
I found this solution helpful for me RxSwift zip operator when one observable can fail
Need to catch error before add to zip
let request1 = usecase.request1().asObservable()
let request2 = usecase.request2().catchErrorJustReturn([]).asObservable() // fetching some not so important data which are just good to have.Return empty array on error
Observable.zip(request1,request2)..observeOn(MainScheduler.instance)
.subscribe(
onNext: { //success code },
onError: { _ in //error code}).disposeBy(bag:myDisposeBag)
But I changed a bit the final:
let request2 = usecase.request2().catchError { _ in
return .just([])
}
I'm facing a problem when selecting the table view row on RxSwift. For details, the code on the do(onNext:) function is called twice, thus lead to the navigation pushed twice too. Here is my code in the viewModel, please help me resolve it. Thanks so much.
struct Input {
let loadTrigger: Driver<String>
let searchTrigger: Driver<String>
let selectMealTrigger: Driver<IndexPath>
}
struct Output {
let mealList: Driver<[Meal]>
let selectedMeal: Driver<Meal>
}
func transform(_ input: HomeViewModel.Input) -> HomeViewModel.Output {
let popularMeals = input.loadTrigger
.flatMap { _ in
return self.useCase.getMealList()
.asDriver(onErrorJustReturn: [])
}
let mealSearchList = input.searchTrigger
.flatMap { text in
return self.useCase.getMealSearchList(mealName: text)
.asDriver(onErrorJustReturn: [])
}
let mealList = Observable.of(mealSearchList.asObservable(), popularMeals.asObservable()).merge().asDriver(onErrorJustReturn: [])
let selectedMeal = input.selectMealTrigger
.withLatestFrom(mealList) { $1[$0.row] }
.do(onNext: { meal in
self.navigator.toMealDetail(meal: meal)
})
return Output(mealList: mealList, selectedMeal: selectedMeal)
}
Edit: Here's the implemetation on the ViewController:
func bindViewModel() {
self.tableView.delegate = nil
self.tableView.dataSource = nil
let emptyTrigger = searchBar
.rx.text.orEmpty
.filter { $0.isEmpty }
.throttle(0.1, scheduler: MainScheduler.instance)
.asDriver(onErrorJustReturn: "")
let loadMealTrigger = Observable
.of(emptyTrigger.asObservable(), Observable.just(("")))
.merge()
.asDriver(onErrorJustReturn: "")
let searchTrigger = searchBar.rx.text.orEmpty.asDriver()
.distinctUntilChanged()
.filter {!$0.isEmpty }
.throttle(0.1)
let selectMealTrigger = tableView.rx.itemSelected.asDriver()
let input = HomeViewModel.Input(
loadTrigger: loadMealTrigger,
searchTrigger: searchTrigger,
selectMealTrigger: selectMealTrigger
)
let output = viewModel.transform(input)
output.mealList
.drive(tableView.rx.items(cellIdentifier: MealCell.cellIdentifier)) { index, meal, cell in
let mealCell = cell as! MealCell
mealCell.meal = meal
}
.disposed(by: bag)
output.selectedMeal
.drive()
.disposed(by: bag)
}
Firstly, is this RxSwift?
If so, the .do(onNext:) operator provides side effects when you receive a new event via a subscription; Therefore, two "reactions" will happen when a table row is tapped: 1. subscription method and 2. .do(onNext:) event. Unfortunately, I do not have any further insight into your code, so there may be other stuff creating that error aswell.
Good luck!
I have the following MVVM-C + RxSwift code.
The problem is that the TableView is not receiving any signals. When I
debug the results I can see that the API call is returning what it should, the objects array is populated with objects but the tableview does not show any results. Here is the console output:
2018-11-13 16:12:08.107: searchText -> Event next(qwerty)
Search something: qwerty
2018-11-13 16:12:08.324: viewModel.data -> Event next([])
Could it be the tableview itself? Maybe wrong custom cell setup?
ViewController.swift:
tableView = UITableView(frame: self.view.frame)
tableView.delegate = nil
tableView.dataSource = nil
tableView.register(SearchResultCell.self, forCellReuseIdentifier: "SearchResultCell")
viewModel.data
.debug("viewModel.data", trimOutput: false)
.drive(tableView.rx.items(cellIdentifier: "SearchResultCell")) { row, object, cell in
cell.name.text = object.name
cell.something.text = object.something
}
.disposed(by: disposeBag)
ViewModel.swift:
let disposeBag = DisposeBag()
var searchText = BehaviorRelay(value: "something to search for")
lazy var data: Driver<[Object]> = {
return self.searchText.asObservable()
.debug("searchText", trimOutput: false)
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest(searchSomething)
.asDriver(onErrorJustReturn: [])
}()
func searchSomething(query: String) -> Observable<[Object]> {
print("Search something: \(query)")
let provider = MoyaProvider<APIService>()
var objects = [Object]()
provider.rx.request(.search(query: query)).subscribe { event in
switch event {
case let .success(response):
do {
let responseJSON: NSDictionary = try (response.mapJSON() as? NSDictionary)!
objects = self.parse(json: responseJSON["results"] as Any)
} catch(let error) {
print(error)
}
break
case let .error(error):
print(error)
break
}
}
.disposed(by: disposeBag)
let result: Observable<[Object]> = Observable.from(optional: objects)
return result
}
When using flatMap, you do not want to create nested subscriptions. You will create an Observable that returns the expected result, and flatMap will take care of subscribing to it. In the current state of things, searchSomething will always return an empty array, as Observable.from(optional: objects) will be called before the request has a chance to complete.
Since version 10.0 of Moya, provider will cancel the requests it created when deallocated. Here, it will be deallocated when execution exits searchSomething, hence the network request won't have time to finish. Moving provider's declaration to the view model's level solves this issue.
Here's searchSomething(query: String) -> Observable<[Object]> rewritten.
let provider = MoyaProvider<APIService>()
func searchSomething(query: String) -> Observable<[Object]> {
print("Search something: \(query)")
return provider.rx.request(.search(query: query)).map { (response) -> [Object] in
let responseJSON: NSDictionary = try (response.mapJSON() as? NSDictionary)!
return self.parse(json: responseJSON["results"] as Any)
}
}
Instead of doing the transformation in subscribe, it's done in map, which will be called for every next event, being passed the value associated with the event.