oyyq99999 / react-native-secure-storage

A secure AsyncStorage implementation
MIT License
74 stars 54 forks source link

MultiGet support ? #7

Open dungnguyen10989 opened 5 years ago

dungnguyen10989 commented 5 years ago

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

woodpav commented 5 years ago

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.