Skip to content

Shared preferences plugin - Way to clear cache or resync #22016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deckerst opened this issue Sep 19, 2018 · 6 comments
Closed

Shared preferences plugin - Way to clear cache or resync #22016

deckerst opened this issue Sep 19, 2018 · 6 comments
Labels
c: new feature Nothing broken; request for a new capability p: shared_preferences Plugin to read and write Shared Preferences package flutter/packages repository. See also p: labels.

Comments

@deckerst
Copy link

It seems there is no way to reinitialize the shared preferences plugin. Its singleton instance is private, and its internal cache cannot be reset.

I'm in a situation where a flutter app and a headless background service read/write preferences with the following scenario:

  1. user opens app
  2. preferences get initialized in app isolate
  3. user leaves app (app stays in memory)
  4. background service wakes up
  5. preferences get initialized in background service isolate
  6. background service modifies preferences
  7. user reopens app
  8. app has obsolete preferences

If I could reset the shared preferences plugin cache when the app resume, it would solve this synchronicity issue.

@zoechi
Copy link
Contributor

zoechi commented Sep 25, 2018

Similar to #17914

Please add the output of flutter doctor -v.

@zoechi zoechi added this to the Goals milestone Sep 25, 2018
@deckerst
Copy link
Author

deckerst commented Sep 27, 2018

flutter doctor -v:
[✓] Flutter (Channel dev, v0.9.1, on Mac OS X 10.13.6 17G65, locale en-KR)
    • Flutter version 0.9.1 at /Users/thibaultdeckers/projects/flutter
    • Framework revision bf7c27095c (8 days ago), 2018-09-18 13:39:21 -0700
    • Engine revision 9e315e6465
    • Dart version 2.1.0-dev.4.0.flutter-050561fd82

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at /Users/thibaultdeckers/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.0, Build version 10A255
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.2

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 28.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] VS Code (version 1.27.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.18.0

[✓] Connected devices (3 available)
    • E5823         • CB5A28Q7EL                           • android-arm64 • Android 7.1.1 (API 25)
    • iPhone 6s     • EA50A2AF-4697-4490-AB44-621D67800C14 • ios           • iOS 12.0 (simulator)
    • iPhone XS Max • 6B15FE2E-717E-4047-A0FF-A2A0BA97D3A1 • ios           • iOS 12.0 (simulator)

• No issues found!

@JosephDunivan
Copy link

Have you tried checking the value and then setting a new value?
Something like this might work to "reset the shared preferences plugin cache"?

  @override
  void initState() {
    super.initState();
      // checks if key is not equal to expected value
      if ((prefs.getInt('key')) != expectedValue) {
        // returns key of expected Value to a new value
        return (prefs.getInt('key') ?? valueYouWantToChangeKeyTo);
      } else {
        // returns value stored in in key
        return (prefs.getInt('key'));
      }
  }

@zoechi zoechi added p: shared_preferences Plugin to read and write Shared Preferences c: new feature Nothing broken; request for a new capability labels Jan 8, 2019
@Guillaume-Fortin
Copy link

in background isolate, i write a data in sharepref.
in my ui , i can't read my data because _preferenceCache contain old value.

this problem is present because sharpref plugin use a cache when i get data.
And background service (with alarmmanager plugin) use an isolate thread, so UI isolate and background isolate don't use same memory usage.

we need in sharepref plugin with a possibility to get data without cache.
example : getInt(String key, bool noCache)

or a getInstance method with renew flag for créate a new instance of cache
like:

static Future<SharedPreferences> getInstance({bool renew}) async {
    if(renew)
    {
      _preferenceCache.clear();
      _instance = null;
    }
    if (_instance == null) {
      final Map<Object, Object> fromSystem =
          // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
          // https://github.com/flutter/flutter/issues/26431
          // ignore: strong_mode_implicit_dynamic_method
          await _kChannel.invokeMethod('getAll');
      assert(fromSystem != null);
      // Strip the flutter. prefix from the returned preferences.
      final Map<String, Object> preferencesMap = <String, Object>{};
      for (String key in fromSystem.keys) {
        assert(key.startsWith(_prefix));
        preferencesMap[key.substring(_prefix.length)] = fromSystem[key];
      }
      _instance = SharedPreferences._(preferencesMap);
    }
    return _instance;
  }

@nioncode
Copy link
Contributor

This has been solved by flutter/plugins#1198, which introduced a reload method that clears the cache and refreshes the underlying data.

This issue should be closed.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: new feature Nothing broken; request for a new capability p: shared_preferences Plugin to read and write Shared Preferences package flutter/packages repository. See also p: labels.
Projects
None yet
Development

No branches or pull requests

5 participants