StorageUtil.js
878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {AsyncStorage} from 'react-native';
export default class StorageUtil {
/**
* 获取
* @param key
* @returns {Promise<T>|*|Promise.<TResult>}
*/
static get(key, callback) {
AsyncStorage.getItem(key, (error, object) => {
callback(error, JSON.parse(object));
})
}
/**
* 保存
* @param key
* @param value
* @returns {*}
*/
static set(key, value, callback) {
return AsyncStorage.setItem(key, JSON.stringify(value), callback);
}
/**
* 更新
* @param key
* @param value
* @returns {Promise<T>|Promise.<TResult>}
*/
static update(key, value) {
StorageUtil.set(key, value);
}
/**
* 删除
* @param key
* @returns {*}
*/
static delete(key) {
return AsyncStorage.removeItem(key);
}
/**
* 清除所有Storage
*/
static clear() {
AsyncStorage.clear();
}
}