0

I've a text file in my assets folder, where it has around 15000 lines, I wanna loop through each line, I've applied the logic using BufferedReader, but it takes too long to loop through each line(1min+).

Now I need to reduce the time of reading each line to maximize the UX.

AsyncTask<Void, String, Void> asyncTask = new AsyncTask<Void, String, Void>() {
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            appBarLayout.setVisibility(View.VISIBLE);
            viewpager.setVisibility(View.VISIBLE);
            splashScreen.setVisibility(View.GONE);
            gamesLoaded = true;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            makeRandomText(gameRandomText, "Random Text" + new Random().nextInt(1000));
        }
        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            gamePercentage.setText(values[0]);
        }
        @Override
        protected Void doInBackground(Void... voids) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(
                        new InputStreamReader(getAssets().open("games.txt"), "UTF-8"));

                int startingCount = 0;
                // do reading, usually loop until end of file reading
                String mLine;
                while ((mLine = reader.readLine()) != null) {
                    Game gameAdded = new Game(mLine);
                    addGame(database, gameAdded);
                    startingCount++;
                    String percentage = startingCount * 100 / Constants.GAMES_COUNT + "%";
                    publishProgress(percentage);
                }
            } catch (IOException e) {
                //log the exception
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        //log the exception
                    }
                }
            }
            return null;
        }
    };

My assets folder

hellohello
  • 65
  • 7
  • Where are the strings going, a database table or in-memory class? Does this happen on each cold start of the game? – Morrison Chang Nov 18 '18 at 23:24
  • Room database, but i really don't think it's the reason as i've tried removing this line and repeating the process, it took around the same time – hellohello Nov 18 '18 at 23:29
  • If @kiskae answer doesn't resolve your issue: I would suggest [Shipping an application with a pre-populated database](https://stackoverflow.com/q/513084/295004), however it looks like Room doesn't specifically support that behavior: [Room library can copy db from asset folder?](https://stackoverflow.com/q/49338939/295004) – Morrison Chang Nov 18 '18 at 23:42
  • What's the code for `publishProgress(percentage)` and how frequently are you updating the UI (i.e. 15000 updates is not useful, make UI update conditional) – Morrison Chang Nov 18 '18 at 23:52
  • publishProgress triggers onProgressUpdate, where it happens to update my percentage to inform the user. According to document: "onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field." – hellohello Nov 18 '18 at 23:55
  • acknowledged error on my part. You should wrap `publishProcess(percentage)` in a conditional so it isn't called 15000 times. – Morrison Chang Nov 19 '18 at 00:00
  • All right, Indeed you were correct about calling UI 15000 times, which was bad and caused time waste, I've fixed this issue by providing switch-case, and it reduced the time, but that wasn't the only issue, I've got problem with room database as it takes too long to insert a game(I tried storing it in arraylist instead of room and it was much faster - 3 or 4 secs), so basically Room & calling UI 15k times were the issue, both you, and @Kiskae had caught the problems which were room & runOnUiThread, Thanks for the help. :) both of u deserve green arrow – hellohello Nov 19 '18 at 00:52

1 Answers1

0

Without knowing how long it takes to execute addGame it is hard to say for sure, but I suspect the issue lies with your use of runOnUiThread. You said your file contains 15000 lines so it will create and schedule an update that the UI handler HAS to run for each item with the way you have it set up.

What you are not aware of is that AsyncTask has a built-in way to handle progress which handles request debouncing for you. Look into the publishProgress and onProgressUpdate methods to handle to UI updates.

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • I've tried your suggestion with onProgressUpdate, yet it doesn't resolve my timing issue, updated the code in the post. – hellohello Nov 18 '18 at 23:53
  • All right, Indeed you were correct about calling UI 15000 times, which was bad and caused time waste, I've fixed this issue by providing switch-case, and it reduced the time, but that wasn't the only issue, I've got problem with room database as it takes too long to insert a game(I tried storing it in arraylist instead of room and it was much faster - 3 or 4 secs), so basically Room & calling UI 15k times were the issue, both you, and @Morrison had caught the problems which were room & runOnUiThread, Thanks for the help. :) both of u deserve green arrow – hellohello Nov 19 '18 at 00:52