Reputation: 177
I am making an app and it has to save and extract data at runtime, and sharedPrefrences is making my animations lag. so is there a way to save and retrieve data on NON-UI thread.
Or is there any problem with my method
private void difficultyHandler(){
sharedPref = this.getSharedPreferences(getString(R.string.lScore),Context.MODE_PRIVATE);
difficulty = sharedPref.getInt(getString(R.string.difficultyController),0);
if (times >= 1){
long latestScore1 = sharedPref.getLong(getString(R.string.lScore1),0);
long latestScore2 = sharedPref.getLong(getString(R.string.lScore2),0);
editor = sharedPref.edit();
editor.putLong(getString(R.string.lScore1),score);
editor.putLong(getString(R.string.lScore2),latestScore1);
if(latestScore1 >= 60 && latestScore2 >=60 && latestScore1 < 140 && latestScore2 <140){
difficulty = 2;
}else if (latestScore1 < 60 && latestScore2 < 60){
difficulty = 1;
}else if(latestScore1 >= 140 && latestScore2 >=140){
difficulty = 3;
}
editor.putInt(getString(R.string.difficultyController),difficulty);
editor.commit();
}
there is one more method like this.
is there a way to put the entire method on another thread.
basically i want to solve the problem of lagging.
Upvotes: 1
Views: 2159
Reputation: 1007286
so is there a way to save and retrieve data on NON-UI thread.
SharedPreferences
are cached. The first time you try accessing a given SharedPreferences
(e.g., getSharedPreferences()
), there will be disk I/O. You are welcome to do this work on a background thread, sometime in advance of when you need the preferences.
You can call apply()
, rather than commit()
, to persist changes to the SharedPreferences
on a framework-supplied background thread.
Upvotes: 5