AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage
system that is global to the app. It should be used instead of LocalStorage.
It is recommended that you use an abstraction on top of AsyncStorage
instead of AsyncStorage directly for anything more than light usage since
it operates globally.
On iOS, AsyncStorage is backed by native code that stores small values in a
serialized dictionary and larger values in separate files. On Android,
AsyncStorage will use either RocksDB or SQLite
based on what is available.
The AsyncStorage JavaScript code is a simple facade that provides a clear
JavaScript API, real Error objects, and simple non-multi functions. Each
method in the API returns a Promise object.
Persisting data:
Fetching data:
Fetches an item for a key and invokes a callback upon completion.
Returns a Promise object.
| Name and Type | Description |
|---|---|
| key string | Key of the item to fetch. |
| [callback] ?(error: ?Error, result: ?string) => void | Function that will be called with a result if found or any error. |
Sets the value for a key and invokes a callback upon completion.
Returns a Promise object.
| Name and Type | Description |
|---|---|
| key string | Key of the item to set. |
| value string | Value to set for the |
| [callback] ?(error: ?Error) => void | Function that will be called with any error. |
Removes an item for a key and invokes a callback upon completion.
Returns a Promise object.
| Name and Type | Description |
|---|---|
| key string | Key of the item to remove. |
| [callback] ?(error: ?Error) => void | Function that will be called with any error. |
Merges an existing key value with an input value, assuming both values
are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
| Name and Type | Description |
|---|---|
| key string | Key of the item to modify. |
| value string | New value to merge for the |
| [callback] ?(error: ?Error) => void | Function that will be called with any error. |
Erases all AsyncStorage for all clients, libraries, etc. You probably
don't want to call this; use removeItem or multiRemove to clear only
your app's keys. Returns a Promise object.
| Name and Type | Description |
|---|---|
| [callback] ?(error: ?Error) => void | Function that will be called with any error. |
Gets all keys known to your app; for all callers, libraries, etc.
Returns a Promise object.
| Name and Type | Description |
|---|---|
| [callback] ?(error: ?Error, keys: ?Array<string>) => void | Function that will be called the keys found and any error. |
Flushes any pending requests using a single batch call to get the data.
This allows you to batch the fetching of items given an array of key
inputs. Your callback will be invoked with an array of corresponding
key-value pairs found:
The method returns a Promise object.
| Name and Type | Description |
|---|---|
| keys Array<string> | Array of key for the items to get. |
| [callback] ?(errors: ?Array<Error>, result: ?Array<Array<string>>) => void | Function that will be called with a key-value array of the results, plus an array of any key-specific errors found. |
Use this as a batch operation for storing multiple key-value pairs. When the operation completes you'll get a single callback with any errors:
The method returns a Promise object.
| Name and Type | Description |
|---|---|
| keyValuePairs Array<Array<string>> | Array of key-value array for the items to set. |
| [callback] ?(errors: ?Array<Error>) => void | Function that will be called with an array of any key-specific errors found. |
Call this to batch the deletion of all keys in the keys array. Returns
a Promise object.
| Name and Type | Description |
|---|---|
| keys Array<string> | Array of key for the items to delete. |
| [callback] ?(errors: ?Array<Error>) => void | Function that will be called an array of any key-specific errors found. |
Batch operation to merge in existing and new values for a given set of
keys. This assumes that the values are stringified JSON. Returns a
Promise object.
NOTE: This is not supported by all native implementations.
| Name and Type | Description |
|---|---|
| keyValuePairs Array<Array<string>> | Array of key-value array for the items to merge. |
| [callback] ?(errors: ?Array<Error>) => void | Function that will be called with an array of any key-specific errors found. |
You can edit the content above on GitHub and send us a pull request!
Examples # | Edit on GitHub |