I am not able to find out the what is the issue , just to filter out response based on status code as follow:
The code should be filter out the response based on the status code & return response & data as observable.
CodePudding user response:
There are few issues with your approach:
- Why are you using
Observable.createwhen you haveURLSession.shared.rx.responsethat is already returningObservable? - Your last
flatMapshould return anObservablebut you are not returning anything from there - There's
URLSession.shared.rx.datathat does thestatusCodecheck for you - You can use
.decodeoperator for decoding - Why it's a generic func that returns
Tbut is namedgetUsers? - You are never subscribing to the inner
ObservableinsideObservable.createso your reactive chain will never run
Given these issue you can adapt your code to:
func getUsers<T: Codable>(urlStr: String) -> Observable<T> {
guard let url = URL(string: urlStr) else { return .empty() }
return URLSession.shared.rx.data(request: .init(url: url))
.decode(type: T.self, decoder: JSONDecoder())
}

