react-native-secure-storage icon indicating copy to clipboard operation
react-native-secure-storage copied to clipboard

MultiGet support ?

Open dungnguyen10989 opened this issue 6 years ago • 1 comments

Hi! Can you provide the method MultiGet like 'react-native/AsyncStorage' ?

dungnguyen10989 avatar Nov 26 '18 04:11 dungnguyen10989

Be warned: You must use SecureStorage sequentially.

This function does not work:

async function multiGet(keys) {
  return await Promise.all(keys.map(SecureStorage.getItem));
}

Use this function instead:

async function multiGet(keys) {
  var acc = {};
  for (var key of keys) {
    await new Promise((res, rej) => setTimeout(res, 100));  // sleep for 100ms
    acc[key] = await SecureStorage.getItem(key);
  }
  return acc;
}

Edit: this applies to getItem and removeItem and may apply to getAllKeys and setItem as well.

Edit 2: it looks like SecureStorage functions cannot be fired in quick succession. There needs to be a delay added.

Edit 3: I suggest using JSON.stringify to store multiple keys if at all possible to avoid the delay.

woodpav avatar Feb 08 '19 18:02 woodpav