0

I need a shorter & cleaner solution for Example 1. So multiple async calls need to be finished before a certain Activity/Fragment can start. Example 1 is very messy and ugly with member bools, but works.

I was considering using the Google Tasks API. But for that I need to add a google-services.json and connect to either "Google Sign-in", "Analytics" or "Cloud messaging", which I don't need I think. There must be a better way or is this the correct way to go?

Example 1:

boolean mIsFirstDone = false;
boolean mIsSecondDone = false;
boolean mAlreadyDone = false;

private void prepareSomeData() {
    dataManager.requestSomeContent(new ApiCallback() {
        @Override
        public void onSuccess(final Object object) {
           mIsFirstDone = true;

           if(mIsFirstDone && mIsSecondDone && !mAlreadyDone) {
               mAlreadyDone = true;
               doSomething();
           }
        }
    });
}

private void prepareSomeSettings() {
    dataManager.requestSomeSettings(new ApiCallback() {
        @Override
        public void onSuccess(final Object object) {
            mIsSecondDone = true;

           if(mIsFirstDone && mIsSecondDone && !mAlreadyDone) {
               mAlreadyDone = true;
               doSomething();
           }
        }
    });
}

With Tasks API:

 Tasks.whenAll(SomeDataTask, SomeSettingsTask).addOnSuccessListener(executor, new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void v) {
            doSomething();
        }
    }).addOnFailureListener(executor, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        }
    });
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94

2 Answers2

2

RxJava, as pointed out, is probably a better solution to this. The reason why is because you can chain multiple api requests, database requests into a concrete block of code that looks elegant and clean. As an example, see below of what I'm trying to say:

Subscription subscription = apiService.getUser(someId)
    .flatMap(user -> apiService.getFavourites(user.getFavouritesTag())
    .subscribe(favourites -> view.updateFavouritesList(favourites), 
        throwable -> Log.e(TAG, throwable.printStackTrace());
blackpanther
  • 10,998
  • 11
  • 48
  • 78
1

Have you considered learning about RxJava and reformatting all your projects to RxJava along with retrofit for API?

start with something like this:

https://medium.com/yammer-engineering/chaining-multiple-sources-with-rxjava-20eb6850e5d9

https://adityaladwa.wordpress.com/2016/05/11/dagger-2-and-mvp-architecture/

tompadre
  • 797
  • 7
  • 22
  • Thnx! I'll look into this during the weekend. Will take some time to figure RxJava out I'm afraid :). – Jim Clermonts May 11 '17 at 09:11
  • eventually, you'll migrate to it whether you like it or not :D You can see it already by answers to your question ;) I'm not sure that you'll get any other acceptable answer but the one with recommending rxjava. – tompadre May 11 '17 at 09:36
  • I'll wait a bit but I'm surprised Tasks API is not a good option since it is by Google especially for Android. – Jim Clermonts May 11 '17 at 11:46