xref: /MusicFree/src/utils/storage.ts (revision cf2d630eca034addcb92030a5268105bce30ef82)
1import {errorLog} from '@/utils/log';
2import AsyncStorage from '@react-native-async-storage/async-storage';
3
4export async function setStorage(key: string, value: any) {
5    try {
6        await AsyncStorage.setItem(key, JSON.stringify(value, null, ''));
7    } catch (e: any) {
8        errorLog(`存储失败${key}`, e?.message);
9    }
10}
11
12export async function getStorage(key: string) {
13    try {
14        const result = await AsyncStorage.getItem(key);
15        if (result) {
16            return JSON.parse(result);
17        }
18    } catch {}
19    return null;
20}
21
22export async function getMultiStorage(keys: string[]) {
23    if (keys.length === 0) {
24        return [];
25    }
26    const result = await AsyncStorage.multiGet(keys);
27
28    return result.map(_ => {
29        try {
30            if (_[1]) {
31                return JSON.parse(_[1]);
32            }
33            return null;
34        } catch {
35            return null;
36        }
37    });
38}
39
40export async function removeStorage(key: string) {
41    return AsyncStorage.removeItem(key);
42}
43