androidperformanceandroid-sqliteandroid-json

Storage and retrieve Data Efficiently


I have a question for people who knows about performance reading stored data:

First of all I would like to explain a bit my App: I have a JSON in App Assets (77kb size) with a list of objects which contains arrays of information like image paths and text to be displayed in my App. In my MainActivity there are different buttons, each button call a view and loads the JSON information in a recycleView.

Problem: The first time I click the button when I start the App, the animation lags and 1 second later it loads the next activity (which displays the information with images, text, etc.). It isn't a really important problem but I don't like it.

Question: Could I use SQLite solve this problem? Read from JSON every time I start the App lags when I click the button which start the activity and create the Objects (I think).

Someone can give me any advice?

JSON stored in App Assets. Static information, never changes.


Solution

  • If the data is static information I think its recommended you use shared preferences. If you are not sharing the configuration between multiple applications you should use shared preferences in private mode to store the JSON and use the shared preferences API along with GSON to extract your data when the activity starts.

    You could access it when your activity starts - override the onCreate method and either run the task in the UI thread or if you really need to an AsyncTask to run a background task.

    If the configuration is used across multiple activities, configuration might be changed by another part of your application e.g another activity. Make sure that when you move back to your initial activity you override onResume or similar to re-get the necessary configuration. Have a read of activity lifecycle documenation if you are unsure about when to access your shared preferences.

    If you are loading or writing a lot of data you might want to try running that outside of your UI thread, using an AsyncTask, otherwise you will block the UI thread which is what causing the lag/blocking.