compte14031879
compte14031879

Reputation: 1591

Callbacks inside a loop : how to know when all is finished?

In a loop, I need to call multiple times a method with callback . How can I know when all is finished ?

@Override
public void onObjectsMustBeParsed(String parsableObjects) {
    String[] parsedObjects = parsableObjects.split(",");

    for (String parsedObject : parsedObjects){

        loadObject(parsedObject, new LoadObjectCallback() {
            @Override
            public void onObjectLoaded(Object object) {
                //Object Loaded
                saveObject(object, new SaveObjectCallback() {
                    @Override
                    public void onObjectSaved() {
                        // Object saved
                    }

                    @Override
                    public void onError() {
                        // Object not saved

                    }
                });
            }

            @Override
            public void onError(Throwable throwable) {
                // Object not Loaded

            }
        });
    }
}


// => do something when all processing of parsed-objects are finished
       // do something if all is ok
       // do other thing if partially ok

Note : To manipulate my data, I use a repository with local and remote data sources. This piece of code is a part of repository.

Upvotes: 0

Views: 726

Answers (3)

vishal_ratna
vishal_ratna

Reputation: 116

The above answers are correct but I went ahead and created a construct that abstracts all these stuff. It is called MultiTriggerBomb

We can set it up like below // Create a bomb.

multiTriggerBomb = MultiTriggerBomb(5, timerDuration) {
   // Code that needs to get fired on explosion
}

Plant the bomb // plant it!

multiTriggerBomb?.plant()

Then whenever the your callback is finished. Call down

multiTriggerBomb?.down("some condition satisfied")

https://betterprogramming.pub/how-to-prevent-code-execution-till-all-triggers-are-down-or-timer-is-expired-989248849392

Upvotes: 0

greenapps
greenapps

Reputation: 11214

Add a volatile integer which indicates the amount of running tasks. Increment when you start a task. Decrement in onObjectLoaded or in onObjectSaved. Then after every decrement check if the task counter is nul.

Upvotes: 1

Gary Bak
Gary Bak

Reputation: 4798

Similar approach to the comments, but using an AtomicInteger instead:

 AtomicInteger countDownLatch = null;

   @Override
   public void onObjectsMustBeParsed(String parsableObjects) {
      String[] parsedObjects = parsableObjects.split(",");

      countDownLatch = new AtomicInteger(parsedObjects.length);
      for (String parsedObject : parsedObjects){

         loadObject(parsedObject, new LoadObjectCallback() {
            @Override
            public void onObjectLoaded(Object object) {
               //Object Loaded
               saveObject(object, new SaveObjectCallback() {
                  @Override
                  public void onObjectSaved() {
                     // Object saved
                     int value = countDownLatch.decrementAndGet();
                     if ( value == 0 ) {
                        // we are done
                     }

                  }

                  @Override
                  public void onError() {
                     // Object not saved
                     int value = countDownLatch.decrementAndGet();
                     if ( value == 0 ) {
                        // we are done
                     }
                  }
               });
            }

            @Override
            public void onError(Throwable throwable) {
               // Object not Loaded

            }
         });
      }
   }

Upvotes: 1

Related Questions