0

I have an app with a sync button. When the user clicks it, it syncs the local Room database with the cloud server's database using retrofit.

I've set it up so the user can only click the button while it's not processing, i.e. Only allow the user to sync if the previous sync operation is already complete.

The sync works great the first time loading the activity. But if the user clicks the button more than once, I get the IllegalStateException error at the allUsersRemoteCall.enqueue() line. Am I missing a step? Like allUsersRemoteCall.cancel() when the sync is complete?

So, how do I allow the user to sync twice without having to reload the Activity??

allUsersRemoteCall = repositoryRemote.getAllUsers(); 
        defineAllUsersRemoteCallback();
        allUsersRemoteCall.enqueue(allUsersRemoteCallback); // IllegalStateException here.

After some reading, I tried using close instead of enqueue:

allUsersRemoteCall = repositoryRemote.getAllUsers(); 
        defineAllUsersRemoteCallback();
        try{
            allUsersRemoteCall.enqueue(allUsersRemoteCallback);
        } catch (IllegalStateException e){
            allUsersRemoteCall.clone().enqueue(allUsersRemoteCallback);
        }

But this seems inelegant and it could potentially cause dual syncing if I'm not careful down the road.

Thanks for the help.

beebopbogo
  • 29
  • 4

0 Answers0