Moya: How to work in Moya 10.0?

Created on 27 Oct 2017  路  2Comments  路  Source: Moya/Moya

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' has no member 'mapObject'.

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)
            })
    }
question rxmoya

Most helpful comment

thank you so much.
it's works for me!

All 2 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoeFerrucci picture JoeFerrucci  路  3Comments

ghost picture ghost  路  3Comments

geraldeersteling picture geraldeersteling  路  3Comments

mwawrusch picture mwawrusch  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments