handle nested observables in RxSwift
Tag : ios , By : dexteryy
Date : March 29 2020, 07:55 AM
should help you out You will want to use flatMapThe closure passed to flatMap will return an observable. flatMap will then take care of un-nesting it, meaning if the closure returns a value of type Observable , and you call flatMap on a value of type Observable, the resulting observable will be Observable (an not Observable>facebookProvider.validate().flatMap { [weak self] _ in
return self?.loginViewModel.retrieveUserData()
}.subscribe { event in
switch event {
// ...
}
}.addDisposableTo(disposeBag)
public func retrieveUserData(token: String) -> Observable<User> {
return Network.provider
.request(.auth(fbToken: token)).retry(5).debug().mapObject(User.self)
}
facebookProvider.validate().flatMap { [weak self] token in
return self?.loginViewModel.retrieveUserData(token: token)
}.subscribe { event in
switch event {
// ...
}
}.addDisposableTo(disposeBag)
|
iOS Rxswift handle CancelButton click in searchBar
Date : March 29 2020, 07:55 AM
This might help you The behavior you are seeing is exactly how the search bar delegate works. Instead of making your network request when textDidEndEditing fires, connect it to searchButtonClicked.
|
Handle Connection Error in UITableView Binding (Moya, RxSwift, RxCocoa)
Tag : ios , By : Priyatna Harun
Date : March 29 2020, 07:55 AM
will be helpful for those in need You aren't handling errors anywhere. I mean you are acknowledging the error in the do operator but that doesn't actually handle it, that just allows it to pass through to the table view, which can't handle an error. Look up the catchError series of operators for a solution. Probably .catchErrorJustReturn([]) will be all you need. fileprivate func getNextState() {
showFullPageState(State.LOADING)
let products = viewModel.getProductListByID(orderGroup: OrderGroup.SERVICES.rawValue)
.share()
products
.catchError { _ in Observable.never() }
.filter { $0.products != nil }
.map { $0.products! }
.bind(to: tableView!.rx.items(cellIdentifier: cellIdentifier, cellType: ProductCell.self)) {
(row, element, cell) in
self.showFullPageState(State.CONTENT)
cell.product = element
}
.disposed(by: bag)
products
.subscribe(onError: { error in
showStatusError(error: error)
self.showFullPageState(State.CONTENT)
})
.disposed(by: bag)
self.tableView?.rx.setDelegate(self).disposed(by: bag)
}
|
How to handle error from api request properly with RxSwift in MVVM?
Tag : swift , By : Mahesh
Date : March 29 2020, 07:55 AM
hop of those help? You can do that by materializing the even sequence: First step: Make use of .rx extension on URLSession.shared in your network call func networkCall(...) -> Observable<[Post]> {
var request: URLRequest = URLRequest(url: ...)
request.httpMethod = "..."
request.httpBody = ...
URLSession.shared.rx.response(request)
.map { (response, data) -> [Post] in
guard let json = try? JSONSerialization.jsonObject(with: data, options: []),
let jsonDictionary = json as? [[String: Any]]
else { throw ... } // Throw some error here
// Decode this dictionary and initialize your array of posts here
...
return posts
}
}
viewModel.networkCall(...)
.materialize()
.subscribe(onNext: { event in
switch event {
case .error(let error):
// Do something with error
break
case .next(let posts):
// Do something with posts
break
default: break
}
})
.disposed(by: disposeBag)
|
error: Segmentation fault: 11 (in target 'RxSwift' from project 'RxSwift')
Date : March 29 2020, 07:55 AM
this one helps. That is for an unhandled case by Xcode and RxSwift. Project target name should not be same as library name i.e. RxSwift. Expected Xcode smart enough dealing that case with appropriate message while installing library. In my case changing project and target name to MyRxSwift fixed the issue as suggested by Karthik.
|