2
votes

I've inherited an RxSwift project which uses Moya. I have a task to move a request from one backend to another. Both backends have their TargetTypes sets up just fine as they're being used extensively elsewhere.

The original request looked like this:

return accessProvider.rx
    .request(request)
    .asObservable()
    .debug()
    .flatMapLatest({ [weak self] (response) -> Observable<Bool> in
        //does some processing of the JSON
        return just(true)
    })
    .catchError({ (error) -> Observable<Bool> in
        return just(false)
    })

This returns just fine and I get the data.

My new request is exactly the same but the only difference is the provider and requests are using the second TargetType. For this one, the requests never takes place (confirmed with Charles). I have checked the TargetType and it looks just fine and performing a non-Rx Moya request works fine, so does an Rx request where I subscribe to it.

Why doesn't it work using flatMapLatest?

I'm new to Rx and very new to Moya so any help is appreciated.

Edit: Debug messages for the first provider:

Refresh token -> subscribed
Refresh token -> subscribed
Refresh token -> Event next(Status Code: 200, Data Length: 773)

Second:

Refresh token -> subscribed

Nothing else

2

2 Answers

4
votes

Make sure you retain the provider or the dependency chain that it holds somewhere. I've had an issue with retainment in the past.

2
votes

The problem is not in the Moya or the TargetType. As you said

... and it looks just fine and performing a non-Rx Moya request works fine, so does an Rx request where I subscribe to it.

Observables in Rx won't be triggered if there are no subscriptions to it.

In your example, you are returning an Observable and if you subscribe to it everything should work fine. This is actually an expected behavior since you don't want to ignore server response, you want to act accordingly. In your case Bool is being returned as an indicator if a request was successful, so maybe you want to let the user know did he succeed in his action or not, or stop loading indicator or something else.