Moya: RxSwift flatMap "Unable to infer closure type in the current context", Migration from 8 to 11

Created on 27 Jun 2018  Â·  2Comments  Â·  Source: Moya/Moya

✨ Moya/RxSwift version 11.0.2, installed via Cocoapods

I am updating my project (from 8.0.5) to use the latest version of Moya (11.0.2) because I have other co-dependencies I want to update, and I am unsure as to how to update the following code in my project:

let provider = RxMoyaProvider<MyAPIService>()
return provider.request(endpoint)
    .filterSuccessfulStatusAndRedirectCodes()
    .mapJSON()
    .flatMap { response -> Observable<Any> in
        // etc
    }

because I am getting this vague compiler error in Xcode on the flatMap line (after changing RxMoyaProvider to MoyaProvider and inserting an rx between provider. and request):

Unable to infer closure type in the current context

I thought maybe it was a matter of changing flatMap to compactMap but then I get:

Value of type 'PrimitiveSequence' has no member 'compactMap'

I also checked to see what the autocomplete uses for flatMap and it gives me:

.flatMap({ (<#Any#>) -> PrimitiveSequence<SingleTrait, R> in
    <#code#>
})

which I'm not sure what to make of…

I am asking this as a question, doubt there's any kind of bug here. I'm not even entirely sure if this is really specific to Moya, or more of an RxSwift thing…

question rxmoya

Most helpful comment

And, based on the providers documentation:

Always remember to retain your providers, as they will get deallocated if you fail to do so.

So I moved the provider to a stored variable in the class containing the network method, and now calls are going through.

I have also developed a better understanding of the distinction between Observables and Singles as a result of this investigation.

All 2 comments

Oh looks like what I am missing is an .asObservable() in there…

Relevant question #1423

FIxes the compiler error, but network requests still not working. Code inside the flatMap not being called at all.

And, based on the providers documentation:

Always remember to retain your providers, as they will get deallocated if you fail to do so.

So I moved the provider to a stored variable in the class containing the network method, and now calls are going through.

I have also developed a better understanding of the distinction between Observables and Singles as a result of this investigation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fenixsolorzano picture fenixsolorzano  Â·  3Comments

gunterhager picture gunterhager  Â·  3Comments

hjzCy picture hjzCy  Â·  3Comments

sunshinejr picture sunshinejr  Â·  3Comments

pedrovereza picture pedrovereza  Â·  3Comments