Skip to content

fix: make DataStore a singleton to prevent IllegalStateException on Android - #793

Open
gcoro wants to merge 1 commit into
oblador:masterfrom
gcoro:master
Open

fix: make DataStore a singleton to prevent IllegalStateException on Android #793
gcoro wants to merge 1 commit into
oblador:masterfrom
gcoro:master

Conversation

@gcoro

@gcoro gcoro commented Apr 10, 2026

Copy link
Copy Markdown

DataStorePrefsStorage creates a new DataStore instance via the preferencesDataStore delegate every time KeychainModule is instantiated. Since Android's DataStore enforces a single-instance-per-file constraint, this was firing the following exception on version 10.0.0 when the module is recreated


java.lang.IllegalStateException: There are multiple DataStores active for the same file: /data/data/<package>/files/datastore/RN_KEYCHAIN.preferences_pb. 

In this PR I replaced the preferencesDataStore property delegate with a companion object singleton using double-checked locking. The DataStore instance is created once via PreferenceDataStoreFactory.create() and reused across all subsequent KeychainModule instantiations.


This should fix issue #777 and #784. 



I tested this fix on my app running RN 0.81.5 and new architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant