Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is that repetitive use of Android Shared Preference storage make effect on performance?

My application using LocationListener in Android Service to get frequent locations updated. Application have some location filter related data stored in shared preference. Requirement is to get frequent location updates as possible as. I retrive data from Shared Preference in onLocationChanged of the listeners. This is my code

public class MyLocationListener implements LocationListener {
    public void onLocationChanged(final Location loc) {
        sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int filterOne = sharedPreferences.getInt("filter_data",100);
        ------
        ------
        //code to process location with filter   
        ------
        ------ 
    }
}

Using above code sharedPreference is used in repetitive manner.

I already tried place filter variables in outside of onLocationChanged, but when service is restarted the values lost and set to zero.

I just want to know is that good practice or not? Should I have to use another option?

like image 988
Amol Patil Avatar asked Oct 29 '25 07:10

Amol Patil


1 Answers

Using above code sharedPreference is used in repetitive manner. I just want to know is that good practice or not?

Accessing SharedPreferences in a repetitive manner is no problem. However in your case you are reinitializing sharedPreferences everytime the event fires. You should just do that once, in the onCreate of your activity or the onCreateView of your fragment.

Same applies to filterOne. If that value is constant, as in it doesn't change, you should only retrieve it once, outside of the onLocationChanged call.


So to answer your question

is that repetitive use of Android Shared Preference storage make effect on performance?

Yes, but only because you reinitialize it every time. If you follow my advice there will be little to no impact on performance.

like image 145
Tim Avatar answered Oct 31 '25 07:10

Tim