Issue
In an Android application I'm developing, I need to create a backup of a file and check on start up if it is identical to a remote file, if they are have different bytes then overwrite the backup with the remote file, afterwards check if they are the same, if they are identical return true. To do this I have the following process:
if(!backupFileExists(){
backupFile.createNewFile();
}
if(!checkBackupAndRemoteFilesAreIdentical()){ <----First Time
if(overwriteBackupFileWithRemoteFile()){
if(checkBackupAndRemoteFilesAreIdentical()){ <---- Second Time
return true;
}
}
}
return false;
The problem is when i run the code with AsyncTask, the second time i run checkBackupAndRemoteFilesAreIdentical()
the value of the backupFile.length()
hasn't updated, so it returns 0 which then returns false.
However if I add Thread.sleep(5000)
the value of the backupFile.length()
has time to update, it is successful and returns true.
Is there anyway to have this work without the Thread.sleep(5000)
?
Solution
This is normal, a network operation will take longer than loading your first activity. If your 1st activity requires this file (or a portion of it) in order to work, then, you need to get it from the server (yes! in an async task) and update an object that your application will rely on. Decouple your application logic/design from source of data.
This problem is a generic one, what we do in general, we get as little data from the server for the first activity to work, then, when the user navigate to other screens, the data will be available at this stage. I would suggest to think about your application data design. Which Server Data you need in every step and how this will be updated. If you are the owner of the server, and you are able to create new endpoints, try to design fast endpoints, less data, expose 'http head' operations to get only the content-length, ... If you have more details on what you're trying to build, I will be more than happy to go through specifics.
Answered By - BlueNimble
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.