I'm using Moya 9.0.0 + RxSwift + ObjectMapper before.
when i update Moya 10.0, the code is not work.
error: Value of type 'PrimitiveSequence
How can I fix it in Moya 10.0?
thanks
extension Observable {
func mapObject<T: Mappable>(type: T.Type) -> Observable<T> {
return self.map { response in
//if response is a dictionary, then use ObjectMapper to map the dictionary
//if not throw an error
guard let dict = response as? [String: Any] else {
throw RxSwiftMoyaError.ParseJSONError
}
return Mapper<T>().map(JSON: dict)!
}
}
func mapArray<T: Mappable>(type: T.Type) -> Observable<[T]> {
return self.map { response in
//if response is an array of dictionaries, then use ObjectMapper to map the dictionary
//if not, throw an error
guard let array = response as? [[String: Any]] else {
throw RxSwiftMoyaError.ParseJSONError
}
return Mapper<T>().mapArray(JSONArray: array)
}
}
}
func getData(parameters: [String : Any]) -> Promise<DataModel>{
return Promise(resolvers: { (result, error) in
networkClientProvider.rx.request(.getData(parameter: parameters))
.filterSuccessfulStatusCodes()
.mapJSON()
.mapObject(type: DataModel.self)
.subscribe(onSuccess:{
print("result = \($0)")
}, onError: {
print("error = \($0)")
})
.disposed(by: disposeBag)
})
}
The return type of request is a Single, not an Observable. You can either define mapObject as an extension on Single, or use asObservable() to convert the Single to Observable:
func getData(parameters: [String : Any]) -> Promise<DataModel>{
return Promise(resolvers: { (result, error) in
networkClientProvider.rx.request(.getData(parameter: parameters))
.asObservable()
.filterSuccessfulStatusCodes()
.mapJSON()
.mapObject(type: DataModel.self)
.subscribe(onSuccess:{
print("result = \($0)")
}, onError: {
print("error = \($0)")
})
.disposed(by: disposeBag)
})
}
thank you so much.
it's works for me!
Most helpful comment
thank you so much.
it's works for me!