跪拜 Guibai
← All articles
Android · App · Client

SharedPreferences vs DataStore: The Hidden Lock That Freezes Android Apps

By Gemini001 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

If your app relies on apply() for frequent config writes, the ANR you're chasing may actually live in onStop() and other lifecycle callbacks, not where you called it. Moving to DataStore eliminates the lock and the full-file rewrite, and Compose/MVI apps get reactive updates without polling.

Summary

SharedPreferences (SP) freezes and ANRs even when you use apply(). SP's SharedPreferencesImpl uses a global lock, so every read and write contends for it. Worse, the underlying XML file cannot be partially updated: changing one boolean re-serializes the entire configuration and rewrites the whole file. When a background write holds the lock, the main thread blocks and can trigger an ANR. apply() does not solve this; it updates memory immediately and queues the disk write, but Android forces pending writes to finish at lifecycle boundaries like onStop(), so the freeze just moves to a less obvious spot.

DataStore abandons locks. It uses an Actor model: all modifications are sent as messages through a Channel, and a single coroutine processes them serially, which eliminates lock contention. Writes are atomic: it writes a temporary file and then replaces the official one, preventing corruption. Reads are cached in memory and updates are pushed via Kotlin Flow, fitting Compose and MVI. The article compares Preferences DataStore (key-value, easy migration) with Proto DataStore (Protocol Buffers, strong typing). The author recommends DataStore for new projects and targeted migration of SP-heavy, high-frequency paths in existing apps.

Takeaways
— SharedPreferences uses a global lock in SharedPreferencesImpl, so all reads and writes on multiple threads contend for the same key.
— SP's XML storage cannot be partially updated; even a single boolean change rewrites the entire file.
— commit() blocks the calling thread until disk write completes, while apply() writes memory immediately and queues the disk write asynchronously.
— Android forces queued apply() writes to finish at lifecycle events like onStop(), which shifts the ANR risk to those callbacks.
— DataStore implements an Actor model with a Channel: all write modifications are sent as messages and processed serially by one coroutine, so no locks are needed.
— DataStore writes atomically by writing to a temporary file and then replacing the official data file, preventing partial corruption.
— DataStore keeps a memory cache and pushes updates via Kotlin Flow, so subscribers don't poll.
— Preferences DataStore is key-value and easier for migrating from SP; Proto DataStore uses Protocol Buffers for strong typing and fixed schemas.
Conclusions

apply() is not async persistence; it's deferred blocking that surfaces at lifecycle boundaries, which is why ANR reports can be near impossible to reproduce.

Serializing all writes through one coroutine makes ordering deterministic but caps throughput; for apps with constant high-frequency writes, the Channel and scheduling overhead can offset the lock-free benefit.

Preferences DataStore keeps the same type-unsafe key-value model as SP; migrating to it buys concurrency stability, not a better data contract, whereas Proto DataStore requires schema design upfront.

Concepts & terms
Actor model
A concurrency design where each actor owns state and processes a message queue serially; threads send messages instead of sharing state, so locks aren't needed. DataStore uses this pattern with a single coroutine handling all read/write operations.
Atomic write
Writing to a temporary file and then renaming/replacing the target only after the write succeeds, so a crash mid-write leaves the old file intact. DataStore uses this to prevent corrupted config files.
Channel
A Kotlin coroutine primitive that acts as a thread-safe queue. DataStore sends all write operations through a Channel, and a single coroutine consumes the queue in order.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗