Search code examples
androidretrofit2rx-javarx-java2

RxJava Observable and retrofit API calls


I am developing a simple github client that retrieves a list of repositories from a particular username.

I have this method in my activity:

private void subscribeRepos(Observable<List<Repository>> repository) {
    disposable.add(repository
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableObserver<List<Repository>>() {
                @Override
                public void onComplete() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(List<Repository> list) {
                    adapter.setItems(list);
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> onNext Called");
                }
            }));
}

This is my Retrofit service:

public class RetrofitService {

private static final String BASE_URL = "https://api.github.com/";
private RepoAPI repoAPI;
private static RetrofitService INSTANCE;

/**
 * Method that returns the instance
 * @return
 */
public static RetrofitService getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new RetrofitService();
    }
    return INSTANCE;
}

private RetrofitService() {
    Retrofit mRetrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl(BASE_URL)
            .build();
    repoAPI = mRetrofit.create(RepoAPI.class);
}

/**
 * Method that returns the API
 * @return
 */
public RepoAPI getRepoApi() {
    return repoAPI;
}

}

And my RepoAPI interface

public interface RepoAPI {

@GET("/users/{user_name}/repos")
Observable<List<Repository>> getRepositories(@Path("user_name") String userName);

}

So, whenever I actively call subscribeRepos(mainViewModel.getRepositories("whateverusername")); onNext is triggered as expected. But if I manually create a new repository on my github account, onNext is not called. Shouldn't onNext be called anytime I add or remove a new repo on my github account?


Solution

  • This is not actually how reactive streams work with network requests using Retrofit. With a network request, once you have subscribed for an event and you receive its data, that's it. The stream is completed (you can check this logging onComplete callback). Although you can make operations like map, switch, concat, and others with it, it is not a "real time" subscription.

    As said here: "Retrofit Network call with RxJava: Use Single : As our API will not give data in a pieces or multiple times. Instead it will emit everything in just one call. So, in case of Observable onCompleted() will follow as soon as onNext() happens."

    If you want something (almost) real time you could schedule a job to make this api call every few minutes (or seconds, or any time period that you want). Be aware with data leaks and thread handling!