Skip to content
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

syncInBackground: false seems to not work and always leave data undefined #241

Open
booboothefool opened this issue Jan 9, 2020 · 4 comments

Comments

@booboothefool
Copy link

booboothefool commented Jan 9, 2020

Hi so I am trying to have my storage reset every 24 hours. Say a user has 10 stamina. Throughout the day, they may use some and run of stamina when it hits 0. After 24 hours, it should reset back to 10 so the user can use it again.

I noticed with syncInBackground: false the data is always undefined. It doesn't seem to wait and sync back to 10 before loading, but is just stuck as undefined. syncInBackground: true does seem to update it back to 10 sometimes though if the app is reopened, but not reliably.

I am finding these behaviors confusing because it seems like I want syncInBackground: false after reading the description, but that doesn't work at all for me, whereas syncInBackground: true is closer to the desired behavior.

Basically I just want the stamina to reset to 10 every 24 hours.

const storage = new Storage({
  // maximum capacity, default 1000
  size: 1000,

  // Use AsyncStorage for RN apps, or window.localStorage for web apps.
  // If storageBackend is not set, data will be lost after reload.
  storageBackend: AsyncStorage, // for web: window.localStorage

  // expire time, default: 1 day (1000 * 3600 * 24 milliseconds).
  // can be null, which means never expire.
  defaultExpires: 1000 * 3600 * 24,

  // cache data in the memory. default is true.
  enableCache: true,
});

// if data not found or is expired, run this
// so should reset the stamina
storage.sync = {
  // The name of the sync method must be the same as the data's key name
  // And the passed params will be an all-in-one object.
  // You can return a value or a promise here
  stamina(params) {
    let {
      // id,
      // syncParams: { extraFetchOption, someFlag },
    } = params;

    storage.save({
      key: 'stamina',
      data: 10,
      expires: 1000 * 3600 * 24,
    });
  },
};
            const stamina = await storage.load({
                key: 'stamina',
                autoSync: true,
                // syncInBackground (default: true) means if data expired,
                // return the outdated data first while invoking the sync method.
                // If syncInBackground is set to false, and there is expired data,
                // it will wait for the new data and return only after the sync completed.
                // (This, of course, is slower)
                syncInBackground: false,
            });
@sunnylqm
Copy link
Owner

You need to explicitly return the value you want in sync method

@booboothefool
Copy link
Author

booboothefool commented Jan 17, 2020

@sunnylqm Thanks!

const storage = new Storage({
  // maximum capacity, default 1000
  size: 1000,

  // Use AsyncStorage for RN apps, or window.localStorage for web apps.
  // If storageBackend is not set, data will be lost after reload.
  storageBackend: AsyncStorage, // for web: window.localStorage

  // expire time, default: 1 day (1000 * 3600 * 24 milliseconds).
  // can be null, which means never expire.
  defaultExpires: 1000 * 3600 * 24,

  // cache data in the memory. default is true.
  enableCache: true,
});
storage.sync = {
  stamina(params) {
    storage.save({
      key: 'stamina',
      data: 10,
      expires: 1000 * 3600 * 24,
    });

    return 10;
  },
};
const stamina = await storage.load({
  key: 'stamina',
  autoSync: true,
  syncInBackground: false,
});        

That gives me the expected result of 10 after 24 hours. It seems to be working for most people, however some of my users are still reporting that it never resets, which means they're stuck so they can't do anything. Are there special cases where it can get stuck and doesn't sync again?

@booboothefool booboothefool reopened this Jan 18, 2020
@sunnylqm
Copy link
Owner

@booboothefool
Copy link
Author

Oh, I am on iOS.

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

No branches or pull requests

2 participants