0

I am developing a download manager app, where I am using lingochamp filedownloader library to download files from different activities.

Here, I want to maintain a history of files downloaded, but according to a discussion here, the library doesn't maintain such list of files.

So, I have thought of populating a List and then using Gson to convert it to json & save it using shared preferences.

Every time a download starts, paused, canceled or gets completed, the json string is convert back to list and then the list is updated and saved back to json. But since there are multiple activities which will be accessing the same list there may be concurrency issues which might result in inconsistency in data.

So, how should I go about developing the same? Or is the above approach incorrect and there is some better approach available?

smprj
  • 63
  • 1
  • 3
  • 9

1 Answers1

0

There should not be concurrency issues when you only access the preferences in between onStart() and onStop() or onResume()/onPause() because only one activity can be in that state. So it could only happen if you use different threads to access the preferences. Even then I think that SharedPreferences might be able to handle it without any changes. At least I am not aware that we ever had problems with that.

Anyway. In case you have concurrency issues you might want to wrap your SharedPreferences in a Singleton Object and access a synchronized instance of your History Object. Then you just save it when someone adds data and change your History Object first for other components to access. Then you only need to read the data from preferences once, when instantiating the instance of your wrapper.

Also I should probably tell you that you should use something like sqlite to save this data, because saving this in preferences goes against what SharedPreferences are meant for and requires you do get,change and then save all the data when you change or add one entry. Then again I won't deny that I have used this approach on more than one occasion and it works fine for small amounts of data.

Hendrik Marx
  • 709
  • 4
  • 9