Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

How to get all stored key-values #70

Open
SaeedZhiany opened this issue Jan 9, 2022 · 3 comments
Open

How to get all stored key-values #70

SaeedZhiany opened this issue Jan 9, 2022 · 3 comments

Comments

@SaeedZhiany
Copy link

Is there any functionality that returns all stored key values at once? I need to get all key-values to show a list to users

@lorenzogatti
Copy link

You can store with a fixed, known key a list of your keys of interest, or a JSON representation of a complex object containing all your data (instead of loose individual items with various keys). In both cases no particular library support is needed.

@SaeedZhiany
Copy link
Author

@lorenzogatti

Thanks for your suggestion. I'm already doing the same. but I rather have library support for such functionality to get all the key -values by calling the function once. I already have to call the get function to retrieve all the keys and then call the get function for every key I retrieved. I think it's not good practice If an app contains lots of key-values. it will make RN bridge busy I guess.

@Balthazar33
Copy link

Balthazar33 commented Dec 23, 2022

@lorenzogatti

That's true.

One workaround for this is to set the keys at the same in both AsyncStorage and EncryptedStorage, and then fetch the values by looping over the keys and calling EncryptedStorage.getItem() for each key. This way at least you won't have to store the keys in a separate file.

For instance:

//Setting the values:
EncryptedStorage.setItem('key', '<actual value>');
AsyncStorage.setItem('key', 'null'); //Dummy value

//Fetching the values:
const keys = await AsyncStorage.getAllKeys();
const keyValueArray= await Promise.all(
        keys .map(async key => {
          const value = await EncryptedStorage.getItem(key);
          return [key, value];
        }),
      );

Not ideal, but works. Especially in those cases where you don't know what the keys are in advance.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants