Skip to content

Commit 47cf28b

Browse files
committed
docs: v2
1 parent ff85b78 commit 47cf28b

File tree

16 files changed

+1394
-5
lines changed

16 files changed

+1394
-5
lines changed

docs/API.md

Lines changed: 582 additions & 0 deletions
Large diffs are not rendered by default.

docs/Installation.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Installation
3+
---
4+
5+
### Install
6+
7+
With npm:
8+
9+
```shell
10+
npm install @react-native-async-storage/async-storage
11+
```
12+
13+
With Yarn:
14+
15+
```shell
16+
yarn add @react-native-async-storage/async-storage
17+
```
18+
19+
With Expo CLI:
20+
21+
```shell
22+
npx expo install @react-native-async-storage/async-storage
23+
```
24+
25+
### Link
26+
27+
#### Android & iOS
28+
29+
Requires **React Native 0.60+**
30+
31+
[CLI autolink feature](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md)
32+
links the module while building the app.
33+
34+
On iOS, use CocoaPods to add the native `RNAsyncStorage` to your project:
35+
36+
```shell
37+
npx pod-install
38+
```
39+
40+
#### Windows
41+
42+
Requires **React Native Windows 0.63+**
43+
44+
[CLI autolink feature](https://microsoft.github.io/react-native-windows/docs/native-modules-autolinking)
45+
links the module while building the app.
46+
47+
#### macOS
48+
49+
Requires **React Native macOS 0.63+**
50+
51+
1. Set `platform :macos, '10.14'` in `macos/Podfile`
52+
2. Install the pods
53+
3. From now on
54+
[CLI autolink feature](https://microsoft.github.io/react-native-windows/docs/native-modules-autolinking)
55+
will link the module while building the app.

docs/Usage.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: usage
3+
title: Usage
4+
sidebar_label: Usage
5+
---
6+
7+
**Async Storage** can only store `string` data. In order to store object data,
8+
you need to serialize it first. For data that can be serialized to JSON, you can
9+
use `JSON.stringify()` when saving the data and `JSON.parse()` when loading the
10+
data.
11+
12+
### Importing
13+
14+
```js
15+
import AsyncStorage from '@react-native-async-storage/async-storage';
16+
```
17+
18+
### Storing data
19+
20+
`setItem()` is used both to add new data item (when no data for given key
21+
exists), and to modify existing item (when previous data for given key exists).
22+
23+
#### Storing string value
24+
25+
```jsx
26+
const storeData = async (value) => {
27+
try {
28+
await AsyncStorage.setItem('my-key', value);
29+
} catch (e) {
30+
// saving error
31+
}
32+
};
33+
```
34+
35+
#### Storing object value
36+
37+
```jsx
38+
const storeData = async (value) => {
39+
try {
40+
const jsonValue = JSON.stringify(value);
41+
await AsyncStorage.setItem('my-key', jsonValue);
42+
} catch (e) {
43+
// saving error
44+
}
45+
};
46+
```
47+
48+
### Reading data
49+
50+
`getItem` returns a promise that either resolves to stored value when data is
51+
found for given key, or returns `null` otherwise.
52+
53+
#### Reading string value
54+
55+
```jsx
56+
const getData = async () => {
57+
try {
58+
const value = await AsyncStorage.getItem('my-key');
59+
if (value !== null) {
60+
// value previously stored
61+
}
62+
} catch (e) {
63+
// error reading value
64+
}
65+
};
66+
```
67+
68+
#### Reading object value
69+
70+
```jsx
71+
const getData = async () => {
72+
try {
73+
const jsonValue = await AsyncStorage.getItem('my-key');
74+
return jsonValue != null ? JSON.parse(jsonValue) : null;
75+
} catch (e) {
76+
// error reading value
77+
}
78+
};
79+
```
80+
81+
### More
82+
83+
For more examples, [head over to API section.](API.md)

docs/advanced/Backup.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Database backup exclusion
3+
---
4+
5+
### Supported platforms:
6+
- **iOS**
7+
- **macOS**
8+
9+
10+
---
11+
12+
Async Storage stores data in `Application Support` directory, which is backed up by iCloud by default.
13+
This can lead to unintentional behavior where data is persisted even after an app re-installation.
14+
15+
Async Storage disables that feature by default.
16+
17+
In order to enable iCloud backup, open your app's `info.plist` in Xcode and add **boolean** entry called **RCTAsyncStorageExcludeFromBackup** and set its value to **NO** (NO as no for exclusion).
18+
19+
Alternatively, you can open `info.plist` in editor and add new entry:
20+
```diff
21+
+ <key>RCTAsyncStorageExcludeFromBackup</key>
22+
+ <false/>
23+
```
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
---
2+
title: Brownfield integration
3+
---
4+
5+
### Supported platforms:
6+
7+
- **iOS**
8+
- **macOS**
9+
- **Android**
10+
11+
---
12+
13+
# iOS
14+
15+
If you're embedding React Native into native application, you can also integrate
16+
Async Storage module, so that both worlds will use one storage solution.
17+
18+
AsyncStorage can be controlled by the hosting app via the delegate on
19+
`RNCAsyncStorage`:
20+
21+
```objc
22+
RNCAsyncStorage *asyncStorage = [bridge moduleForClass:[RNCAsyncStorage class]];
23+
asyncStorage.delegate = self;
24+
```
25+
26+
## The protocol
27+
28+
The delegate must conform to the `RNCAsyncStorageDelegate` protocol:
29+
30+
### allKeys
31+
32+
```objc
33+
- (void)allKeys:(RNCAsyncStorageResultCallback)block;
34+
```
35+
36+
Returns all keys currently stored. If none, an empty array is returned.
37+
Called by `getAllKeys` in JS.
38+
39+
### mergeValues
40+
41+
```objc
42+
- (void)mergeValues:(NSArray<NSString *> *)values
43+
forKeys:(NSArray<NSString *> *)keys
44+
completion:(RNCAsyncStorageResultCallback)block;
45+
```
46+
47+
Merges values with the corresponding values stored at specified keys.
48+
Called by `mergeItem` and `multiMerge` in JS.
49+
50+
### removeAllValues
51+
52+
```objc
53+
- (void)removeAllValues:(RNCAsyncStorageCompletion)block;
54+
```
55+
56+
Removes all values from the store. Called by `clear` in JS.
57+
58+
### removeValuesForKeys
59+
60+
```objc
61+
- (void)removeValuesForKeys:(NSArray<NSString *> *)keys
62+
completion:(RNCAsyncStorageResultCallback)block;
63+
```
64+
65+
Removes all values associated with specified keys.
66+
Called by `removeItem` and `multiRemove` in JS.
67+
68+
### setValues
69+
70+
```objc
71+
- (void)setValues:(NSArray<NSString *> *)values
72+
forKeys:(NSArray<NSString *> *)keys
73+
completion:(RNCAsyncStorageResultCallback)block;
74+
```
75+
76+
Sets specified key-value pairs. Called by `setItem` and `multiSet` in JS.
77+
78+
### valuesForKeys
79+
80+
```objc
81+
- (void)valuesForKeys:(NSArray<NSString *> *)keys
82+
completion:(RNCAsyncStorageResultCallback)block;
83+
```
84+
85+
Returns values associated with specified keys.
86+
Called by `getItem` and `multiGet` in JS.
87+
88+
### passthrough
89+
90+
```objc
91+
@optional
92+
@property (nonatomic, readonly, getter=isPassthrough) BOOL passthrough;
93+
```
94+
95+
**Optional:** Returns whether the delegate should be treated as a passthrough.
96+
This is useful for monitoring storage usage among other things. Returns `NO` by
97+
default.
98+
99+
---
100+
101+
# Android
102+
103+
The recommended approach here is to use Kotlin language to leverage coroutines when accessing the storage.
104+
Java is also supported (through Kotlin interop), but the approach is more cumbersome.
105+
106+
## Prerequisites
107+
108+
1. [Next storage feature](Next.md) enabled.
109+
2. Add dependency on `coroutines-android` in your app's `build.gradle`
110+
111+
```diff
112+
113+
dependencies {
114+
// other dependencies
115+
116+
117+
// will work with coroutines 1.3.0 and up
118+
+ implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
119+
120+
}
121+
```
122+
123+
3. Your library of choice for parsing JSON storage values (since there are strings only)
124+
125+
## Access storage
126+
127+
### Kotlin (recommended)
128+
129+
We use Coroutines to handle asynchronous code. Each method on storage is `suspend` method, so you need to
130+
call it from within a coroutine.
131+
132+
#### Reading value
133+
134+
```kotlin
135+
suspend fun readValue(ctx: Context, keys: List<String>) {
136+
// get instance of the Storage by providing context object
137+
val asyncStorage = StorageModule.getStorageInstance(ctx)
138+
139+
val entries: List<Entry> = asyncStorage.getValues(keys)
140+
doSomethingWithValues(entries)
141+
}
142+
```
143+
144+
#### Saving value
145+
146+
```kotlin
147+
suspend fun saveValue(ctx: Context) {
148+
val asyncStorage = StorageModule.getStorageInstance(ctx)
149+
150+
val entries = listOf(
151+
Entry("myKey", "myValue")
152+
)
153+
asyncStorage.setValues(entries)
154+
}
155+
```
156+
157+
### Java
158+
159+
You can access AsyncStorage form Java, but you're still required to add Kotlin dependencies.
160+
There's no one way of accessing the data and there's more than one way to parse it.
161+
162+
#### Reading from storage
163+
164+
```java
165+
void readStorageValue(Context ctx, String key) {
166+
AsyncStorageAccess asyncStorage = StorageModule.getStorageInstance(ctx);
167+
168+
BuildersKt.launch(GlobalScope.INSTANCE,
169+
Dispatchers.getIO(),
170+
CoroutineStart.DEFAULT,
171+
(scope, continuation) -> {
172+
List<String> keys = new ArrayList<>();
173+
keys.add(key);
174+
175+
Continuation<? super List<? extends Entry>> cont = new Continuation() {
176+
@NotNull
177+
@Override
178+
public CoroutineContext getContext() {
179+
return scope.getCoroutineContext();
180+
}
181+
182+
@Override
183+
public void resumeWith(@NotNull Object o) {
184+
List<Entry> entries = (List<Entry>) o;
185+
doSomethingWithEntries(entries);
186+
}
187+
};
188+
189+
asyncStorage.getValues(keys, cont);
190+
return Unit.INSTANCE;
191+
});
192+
193+
}
194+
```
195+
196+
#### Saving to storage
197+
198+
```java
199+
void saveStorageValue(Context ctx, String key, String value) {
200+
AsyncStorageAccess asyncStorage = StorageModule.getStorageInstance(ctx);
201+
202+
BuildersKt.launch(GlobalScope.INSTANCE,
203+
Dispatchers.getIO(),
204+
CoroutineStart.DEFAULT,
205+
(scope, continuation) -> {
206+
Continuation cont = new Continuation() {
207+
@NotNull
208+
@Override
209+
public CoroutineContext getContext() {
210+
return scope.getCoroutineContext();
211+
}
212+
213+
@Override
214+
public void resumeWith(@NotNull Object o) {}
215+
};
216+
217+
List<Entry> entries = new ArrayList<>();
218+
Entry entry = new Entry(key, value);
219+
entries.add(entry);
220+
asyncStorage.setValues(entries, cont);
221+
return Unit.INSTANCE;
222+
});
223+
}
224+
```

0 commit comments

Comments
 (0)