0

I have IntentService which I use for uploading photo files to server sequentially. I have Gallery fragment showing files from upload queue and files already uploaded to server. I get list of already uploaded files using api-call to server. Local files which going to be uploaded just stored in DB.

Once next file is uploaded - IntentService sends event to Gallery fragment. After that I can hide progress-wheel for uploaded file and remove record about that file from DB. This part work well.

For synchronization reasons I want to block all events coming from upload Service (make them wait) while api-request for fetching remote files are in progress.

What I'm worry about - some file can be uploaded at the same time with requesting remote files and I will loose that event. How can I delay delivering events from Service until api-request is finish?

Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
Ruslan
  • 1,039
  • 1
  • 9
  • 16
  • How you get event from service to fragment ? via interface ? –  Mar 19 '16 at 09:40
  • @penguin, The thing is that I call API request asynchronously. I receive results in callback, unfortunately the method you have provided won't work. – Ruslan Mar 20 '16 at 12:53

1 Answers1

0

In your fragment you can do this:

boolean isRequestingAPI = false;
.
.
.

//Requesting API on server

isRequestingAPI = true

synchronize(isRequestingAPI){
   // do request API here


   //You get callback from API 
   isRequestingAPI = false;
}

.
.
.

//Getting event from Service

private void onEventFromServceListener(){

   if(isRequestingAPI){
       //Put event/data in a queue and consume it when when you get callback from API
   }
   synchronize(isRequestingAPI){
       //do whatever your want to do with event from service
   }
}

By using synchronize(isRequestingAPI) your API call and Event from service will become mutually exclusive events. Each event will wait for other to finish first.

  • The thing is that I call API request asynchronously. I receive results in callback, unfortunately the method you have provided won't work. – Ruslan Mar 20 '16 at 12:50
  • I have updated my answer. Go with my solution and create a queue. If any event from service arrives in between API call and its callback put it in queue and consume it when Callback from API arrives. –  Mar 20 '16 at 13:51