I have 1000 lines in a file which will be served to the user every time he/she loads the application.
My current approach is:
MainActivity: onCreate: Start an AsyncTask
AsyncTask onPreExecute: show progress dialiog
AsyncTask doInBackground: Check if the key/value is present in sharedpreferences, If yes, then do nothing in doInBackground. If no (first time user), read from the raw file and create a stringbuilder. Store the content of StringBuilder as key value pair in sharedpreferences.
AsyncTask onPostExecute: populate textview from sharedpreferences. Dismiss the progress dialog.
The code to read from file in the doInBackground method is:
StringBuilder sb = new StringBuilder();
InputStream textStream = getBaseContext().getResources().openRawResource(R.raw.file);
BufferedReader bReader = new BufferedReader(new InputStreamReader(textStream));
String aJsonLine = null;
try {
while ((aJsonLine = bReader.readLine()) != null) {
sb.append(aJsonLine + System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bReader.close();
textStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I am seeing that the user has to wait for around 9-10 seconds for first launch and 4-5 seconds for subsequent launches. Any suggestions to improve the performance in my case.