feresr feresr - 1 year ago 97
Android Question

Retrofit observable works only once

I understand that, by default, observables created by retrofit are "cold" observables.

I have this specific call to my server endpoint

Observable<Token> signIn(@Field("username") String username, @Field("password") String password);

When I do:

public class LoginUseCase extends Subscriber<Profile> {
public void logIn(String username, String password) {
Subscription subscription = myApi.signIn(username, password)

I receive the
callbacks, as you would expect.

The problem arises when the login fails and I try again. Calling the
method a second time doesn't trigger the http call, and I don't get any callbacks.

By the way, Im doing this on my

public void onCompleted() {

Is there a way to tell retrofit/rxandroid to re make the http call everytime I call
. Am I even approaching this the right way?

- Im using dagger2 in my project and the myApi object is a singleton.
- I'm able to reproduce the error even when I use different username/pass configs between the first and second try

Answer Source

Once Subscriber#unsubscribe() is called that subscriber can never receive new values. You will need to recreate your subscriber each time you want to subscribe to a new observable.

What is happening is that in the call to Subscriber#add(Subscription) it sees that the subscriber has already been unsubscribed and immediately cancels the new subscription.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download