Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences Editor commit takes so much time

I try to make a configuration activity using PreferenceActivity...

I found some working examples like

"WiFi Advanced Configuration Editor"

and

"Wifi Config Editor Pro"

but the code I wrote waits for 10-15 seconds on the line editor.commit()... it must be very simple but I cant figure out.

here is the brief code;

...

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(v.getContext());
prefs.registerOnSharedPreferenceChangeListener(ClassConfig.this);

    SharedPreferences.Editor editor = prefs.edit();
editor.clear();

editor.putString("key1", value1);
editor.putString("key2", value2);
editor.putBoolean("key3", value3);
    ...
    //i got nearly 35 keys here
    ...
    editor.putString("key33", value33);
editor.putBoolean("key34", value34);
editor.putBoolean("key35", value35);

    editor.commit();

Any ideas??

Update: one more thing. I saw these warnings in the log file

W/BackupManagerService(1914) dataChanged but no participant pkg='com.android.providers.settings' uid=10046

like image 252
caw Avatar asked Feb 04 '11 08:02

caw


3 Answers

commit() is executed synchronously, so you notice that it takes so much time.. Use apply() instead.

https://stackoverflow.com/a/5960743/1233659

like image 100
Hamzeh Soboh Avatar answered Nov 18 '22 12:11

Hamzeh Soboh


Committing large preferences is slow - it should be done in separate thread. Consider implementing this in AsyncTask

like image 42
pixel Avatar answered Nov 18 '22 13:11

pixel


You should use apply() method which is asynchronous. See docs here

like image 2
user3712755 Avatar answered Nov 18 '22 13:11

user3712755