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?
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.