2

I have written program similar to below mention program.

public class UploadingService extends Service {

public UploadingService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        for(int i = 0; i < mArrayUri.size(); i++){
            *********
            //firebase uploading codes
            ************
            /*what code  ??? wait until uploading 
            is complete than for loop iterate new iteration 
            */
        }
  }
}

i want for loop to wait until current uploading is completed then it goes to another iteration.

Shiva.AK
  • 29
  • 8

2 Answers2

1

You can solve this with ObservableInteger by JML. What it does is simply adds numbers. When it reaches the number you want, a task will take process or will finish. Here is how to implement it.

In your OnCreate you just call the listener that will be waiting until something happens

private ObservableInteger mObsInt;

//Listener
mObsInt = new ObservableInteger();
mObsInt.set(0);

mObsInt.setOnIntegerChangeListener(new OnIntegerChangeListener()
{
    @Override
    public void onIntegerChanged(int newValue)
    {
        if (mObsInt.get()==1) {
            Log.e("Uploading Process finished"," mObsInt 1");
            Log.e("Download1"," Finished first process ");

            //Here you can launch your loop method
        }

        if (mObsInt.get()==2) {           
            //Here it will tell you that the loop has finished    
        }
    }
});

So, now you only need to tell mObsInt when to increment, so after finishing UploadingService it will be 1 and then after the loop it will be 2.

To do this add just this line

mObsInt.set(mObsInt.get()+1);

Implementing it would look like this:

public class UploadingService extends Service {

public UploadingService() {

//process

mObsInt.set(mObsInt.get()+1); //now mObsInt will be 1 

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        for(int i = 0; i < mArrayUri.size(); i++){
            *********
            //firebase uploading codes
            ************
            /*what code  ??? wait until uploading 
            is complete than for loop iterate new iteration 
            */
   mObsInt.set(mObsInt.get()+1); //Now mObsInt will be 1 + 1 = 2 , so the code in your onCreate will manage what to do
        }
  }
}

PS: if the uploadingProcess has an AsyncTask , put mObsInt.set(mObsInt.get()+1); in your onPostExecute method

Hope it helps

Happy coding

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • You need to enable data binding in gradle (See https://stackoverflow.com/a/45073778/461982) before Android's ObservableInt will be available, but OnIntegerChangeListener does not exist as part of ObservableInt. You likely implemented the custom class described at https://stackoverflow.com/a/26745209/461982 and mislabeled it. – Abandoned Cart Jun 20 '19 at 01:40
1

"waiting" for another task to complete is really something you want to avoid, especially on android. You should check the basics of asynchronous programming, but there are so many different ways to implement it is hard to give only one direction. Basically, your task thread should notifiy when it is done, which would trigger further treatment, be it with a UI modification or not. You could use an event bus, a post to handler mechanism, a RX pattern, a broadcast Intent and of course an asynctask etc...

For example, you could use an asynctask to upload your content in a list. It executes in another thread but you can always give feedback like between each upload to refresh the UI (progressbar). This would be non-blocking regarding the app, which would allow the user to stop the task. However you need to pay special attention to memory leaks and configuration changes ;)

caketuzz
  • 126
  • 4