Gaijin
Gaijin

Reputation: 61

What is the fastest way to save set of strings in SharedPreferences?

I have 2 data sets

String[] wordsArray;
Queue<String> wordsQueue;

They store the same data, around 500 strings each, 1-3 words per string. I need to save one of them to SharedPreference. What is the best (fastest) way to do it?

Now I just use

Set<String> mySet = new HashSet<String>(wordsQueue);
edit.putStringSet("Words", mySet);

But it works slower than I want.

Upvotes: 3

Views: 1781

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

Use apply() instead of commit() which will save the preference in a background thread (i.e. asynchronous).

Set<String> mySet = new HashSet<String>(wordsQueue);
edit.putStringSet("Words", mySet).apply();

For saving the array of String in SharedPreference you might consider doing something like this stated in this answer.

Upvotes: 5

Related Questions