Fix SharedPreference storage manager bugs and improve consistency#27
Open
shammond42 wants to merge 4 commits intoaptabase:mainfrom
Open
Fix SharedPreference storage manager bugs and improve consistency#27shammond42 wants to merge 4 commits intoaptabase:mainfrom
shammond42 wants to merge 4 commits intoaptabase:mainfrom
Conversation
The command dart pub get, made some updates to external package versions.
Without a consistent namespacing of the keys (such as in init) there could be collisions with SharedPreferences settings from other packages or the app itself. This commit moves the namspacing into storage_manager_shared_prefs.dart and applies it to every key reference.
1. Update the _events map first, so that any errors accessing SharedPreferences do not prevent memory from being upated. 2. Add some error handling around SharedPreferences calls to avoid interrupting the flow if it isn’t necessary.
Get the shared preferences instance once during init. This prevents an async await call with every event.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes several bugs and improves the reliability of
StorageManagerSharedPrefs. The changes address key collision issues, race conditions, and adds error handling to maintain consistency between in-memory and persistent storage.Issues Fixed
1. Key Collision Bug (Critical)
Problem:
init()loaded ALL keys from SharedPreferences, not just Aptabase events. Since SharedPreferences is a global store, this caused unrelated application data to be loaded into the events map.Solution: Added filtering to only load keys with the
aptabase_prefix.2. Improve Encapsulation
Problem: The
aptabase_prefix was defined inaptabase_flutter.dartand used instorage_manager_shared_prefs.dart, coupling the main SDK to storage implementation details.Solution: Moved all prefix handling into
StorageManagerSharedPrefs. The class now adds/removes the prefix internally, and the main SDK passes unprefixed keys._keyPrefixconstant inStorageManagerSharedPrefsinit()to strip prefix when loadingaddEvent()anddeleteEvents()to add prefix when persistingaptabase_from key generation inaptabase_flutter.dartBenefits: Different
StorageManagerimplementations can now use different prefixes or none at all.3. Race Condition Risk
Problem: In-memory state was updated before disk persistence completed. If persistence failed, in-memory and disk state would be inconsistent.
Solution: Persist to disk first, only update memory on success.
4. Add Error Handling
Problem: SharedPreferences operations could fail (disk full, permissions issues) with no recovery mechanism, leaving state inconsistent.
Solution: Added try-catch blocks with appropriate error handling:
addEvent(): Rethrows exceptions so callers know the operation failed. Events not persisted are not added to memory.deleteEvents(): Silently catches errors since events were already sent successfully. Orphaned keys are cleaned up on nextinit().5. Performance - Cache SharedPreferences
Problem: Every
addEvent()anddeleteEvents()call invokedSharedPreferences.getInstance().Solution: Cache the instance during
init()usinglate final:Benefits:
Breaking Changes
None. The API surface remains identical. Changes are internal implementation improvements.