Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

Does modifying a SharedPreferences instance result in an immediate I/O operation on Android?

In my application I modify a SharedPreferences instance every time I finish / start and Activity. I only modify 1 property in it (tho I don't think that it matters now). I was wondering if these modifications are persisted to disk with some file I/O operations right after commit, or Android batches these operations somehow?

I'm thinking about battery life here, as afaik file I/O is slow (especially for writing operations) and is hard on the battery. I couldn't find anything about this in the docs, does anyone know something about this?

Thank you

Upvotes: 0

Views: 290

Answers (3)

Yar
Yar

Reputation: 4561

I'd say the SharedPreferences are stored immediately when

commit;

is called. Looks like Android OS philosophy is to rely on the apps (and their developers) to store their data whenever they want, and in the documentation an accent is put to persist your data in onPause / onDestroy yourself, not rely on the system to do anything for you, if I make myself clear.

Upvotes: 1

Robby Pond
Robby Pond

Reputation: 73484

Yes. The only difference between commit() and apply() is commit is synchronous.

Upvotes: 1

Reuben Scratton
Reuben Scratton

Reputation: 38707

Yes, committing changes to a preferences file will cause the file to be written synchronously. (see source).

Upvotes: 2

Related Questions