3

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

Leon
  • 3,614
  • 1
  • 33
  • 46

2 Answers2

6

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

Timofey Solonin
  • 1,393
  • 13
  • 20
  • This was it, that was the only difference between the two providers, one was an instance variable. Thank you so much! – Leon Apr 11 '18 at 14:03
2

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.

markoaras
  • 96
  • 4
  • As per my question, the code I posted above works perfectly fine for one of the providers but not for the other. – Leon Apr 11 '18 at 09:38
  • can you show you TargetType definition and what is the output of the `debug` – markoaras Apr 11 '18 at 09:42
  • Added debugs. The TargetType is not really possible to post as it contains mostly proprietary info. However, I have checked it over and over and it has the same content as the first. – Leon Apr 11 '18 at 09:54