Dark Mode
Image

AsyncStorage Methods

There are various methods of React Native AsyncStorage class which are described below:

Methods

setItem()

static setItem(key: string, value: string, [callback]: ?(error: ?Error)=>void)  

The setItem() sets the value for a key and invokes a callback upon compilation. It returns a Promise object.

getItem()

   static getItem(key: string, [callback]: ?(error: ?Error, result: ?     string)) =>void)  

The getItem() fetches an item from a key and invokes a callback upon completion. It returns a Promise object.

removeItem()

static removeItem(key: string, [callback]: ?(error: ?Error) => void)  

The removeItem() removes an item for a key and invokes a callback upon compilation. It returns a Promise object.

mergeItem()

static mergeItem(key: string, value: string, [callback]: ?(error: ?Error) => void)  

The mergeItem() merges the existing key's value with the input value and assuming both values are stringified JSON. It returns a Promise object.

NOTE: This method is not supported by all the native implementations.

clear()

static clear([callback]: ?(error: ?Error) => void)  

The clear() method erases all AsynchStorage from all clients, libraries, etc. It is suggested that don't call this, instead of this you may use removeItem or multiRemove to clear only your app's keys. It returns the Promise object.

getAllKeys()

static getAllKeys([callback]: ?(error: ?Error, keys: ?Array) => void)  

It gets all the keys which are known to your app, for all callers, libraries, etc. It returns a Promise object.

flushGetRequests()

static flushGetRequests(): [object Object]  

It flushes any pending request using a single batch call to get the data.

multiGet()

static multiGet(keys: Array, [callback]: ?(errors: ?Array, result: ?Array>) => void)  

This method allows you to batch fetching of items given in an array of key inputs. The callback method will be invoked with an array of corresponding key-value pairs found:

multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])  

The method returns a Promise object.

multiSet()

static multiSet(keyValuePairs: Array>, [callback]: ?(errors: ?Array) => void)  

This method is used as a batch operation to store multiple key-value pairs. After the completion of operations, you will get a single callback with any errors:

multiSet([['k1', 'val1'], ['k2', 'val2']], cb);  

The method returns a Promise object.

multiRemove()

static multiRemove(keys: Array, [callback]: ?(errors: ?Array) => void)  

This method calls the batch deletion of all keys in the key array. It returns a Promise object.

multiMerge()

static multiMerge(keyValuePairs: Array>, [callback]: ?(errors: ?Array) => void)  

It executes the batch of operation to merge existing and new values for a given set of keys. It assumes that the values are stringified JSON. It returns a Promise object.

Note: This method is not supported by all native implementations.

Comment / Reply From