sunnylqm / react-native-storage

local storage wrapper for both react-native and browser. Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
MIT License
3.01k stars 268 forks source link

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

Open booboothefool opened 4 years ago

booboothefool commented 4 years ago

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 commented 4 years ago

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

booboothefool commented 4 years ago

@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?

sunnylqm commented 4 years ago

May be related https://github.com/react-native-community/async-storage/blob/LEGACY/docs/advanced/DedicatedExecutor.md

booboothefool commented 4 years ago

Oh, I am on iOS.