We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recently, I try to use RxSwift + Moya + Decodable to do my project. But I can't find a better Decodable extension for supporting Moya.
So I try to inplement it by myself.
Perhaps it should be written in other better ways, such as naming can have a better way. I'm not sure:
import Foundation import Moya import RxSwift import Decodable extension ObservableType where E == Response { public func mapObject<T: Decodable>(type: T.Type) -> Observable<T> { return flatMap { response -> Observable<T> in return Observable.just(try response.mapObject()) } } public func mapObjectArray<T: Decodable>(type: T.Type) -> Observable<[T]> { return flatMap { response -> Observable<[T]> in return Observable.just(try response.mapObjectArray()) } } } extension Moya.Response { public func mapObject<T: Decodable>() throws -> T { return try T.decode(try mapJSON()) } public func mapObjectArray<T: Decodable>() throws -> [T] { return try [T].decode(try mapJSON()) } }
So it can use like this:
let provider = RxMoyaProvider<Api>() provider.request(.requestSomething) .mapObject(type: DecodableObject.self) .subscribe { event in switch event { case .next(let element): print("next: \(element)") case .error(let error): print("error: \(error)") case .completed: print("completed") } } .addDisposableTo(disposeBag)
The text was updated successfully, but these errors were encountered:
I forgot to add rootKey: String = nil for every methods...
rootKey: String = nil
Sorry, something went wrong.
No branches or pull requests
Recently, I try to use RxSwift + Moya + Decodable to do my project. But I can't find a better Decodable extension for supporting Moya.
So I try to inplement it by myself.
Perhaps it should be written in other better ways, such as naming can have a better way. I'm not sure:
So it can use like this:
The text was updated successfully, but these errors were encountered: