Fix SharedPreference storage manager bugs and improve consistency #27
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.