1

I'm currently writing an application which displays items within a ListView which then users can either add to or delete from. I am making this data persistent by writing out to a local JSON file (I'm saving the data as a JSONArray and then calling the toString() method before Serializing it and writing it, reading is basically the same process reversed).

Since I'm fairly new to developing for Android I'm not very clear on the best practices, and specifically I'm interested in what is the best method for accessing data stored within local files and resource files. Currently I'm Reading and writing from these using an AsyncTaskLoader and am wondering if that's sensible for a local file or is not needed/recommended for local files. Is it OK to run on the main thread for accessing local files or is there some other class besides AsyncTaskLoader I can use?

My main problem is that after implementing the AsyncTaskLoader I am seeing some performance issues and being fairly new to development as a whole I suspect I'm not implementing this fairly complex class in an efficient way. If I can avoid it all together or substitute a less complex class to reach a similar result I think I'd be set. Thanks in advance for feedback and if having some code snippets would help clarify the question please let me know.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43

2 Answers2

1

After searching around for a bit and trying a few different things I found that implementing a SQLLight Database and Cursor Loader was alot faster than trying to Serialize and De-serialize strings to JSON and vice-versa. Also when everything was setup it was a lot easier to implement.

This project is a pretty good example of how to implement this https://github.com/udacity/ud845-Pets .

-1

If your Data file is to big to read and write then you need to use some Thread or AsyncTask. AsyncTask has mainly three methods for batter performance with android UI .

for storing data you can store your json String into SharedPreferences.

see below links for

AsyncTask

And

SharedPreferences

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • `AsyncTask has mainly three methods for batter performance with android UI`. No. – Marcin Orlowski Mar 01 '17 at 08:13
  • better performance volly or retrofit or any other tool is there – android_jain Mar 01 '17 at 08:14
  • The data I have isn't terribly large at the moment and I doubt they'll get much larger than a couple of MB even when complete. Does the methodology for storing/retrieving data change significantly based on file size? – Adrian Stratienco Mar 01 '17 at 09:03
  • @MarcinOrlowski So why Android have AsyncTask and its three method to handle any big operation. – Chetan Joshi Mar 01 '17 at 09:20
  • @android_jain why we use Volly or Retrofit for non Network operation. – Chetan Joshi Mar 01 '17 at 09:22
  • You apparently do not understand how AsyncTask works, what is thread separation or UI thread. I assume you refer to `onPreExecute()`, `onPostExecute()` and `onProgressUpdate()` - these are here not for performance reasons. – Marcin Orlowski Mar 01 '17 at 09:42
  • @MarcinOrlowski Yes off course but my focus is on doInBackground() .do your task in doInBackground() . And if there is long operation whcih we put on main Thread then it will stuck our screen and its leads to bad performance rather its batter to use AsyncTask to do your task in background and show progress dialog. – Chetan Joshi Mar 01 '17 at 09:44