refactor(storage): 迁移 AsyncStorage 至 expo-sqlite/kv-store
- 统一替换所有 @react-native-async-storage/async-storage 导入为自定义 kvStore - 新增 kvStore.ts 封装 expo-sqlite/kv-store,保持与 AsyncStorage 完全兼容 - 新增同步读写方法,提升性能 - 引入 expo-sqlite 依赖并更新 lock 文件 BREAKING CHANGE: 移除 @react-native-async-storage/async-storage 依赖,需重新安装依赖并清理旧数据
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { STORAGE_KEYS } from '@/services/api';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import { resetAllGuides } from './guideHelpers';
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
|
||||
// 引导状态存储键
|
||||
const GUIDE_KEYS = {
|
||||
|
||||
23
utils/kvStore.ts
Normal file
23
utils/kvStore.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// 直接使用 expo-sqlite/kv-store 作为 AsyncStorage 的替代品
|
||||
// 这是 expo-sqlite 提供的现成 key-value store 实现,完全兼容 AsyncStorage API
|
||||
import Storage from 'expo-sqlite/kv-store';
|
||||
|
||||
// 重新导出所有方法,保持与原 AsyncStorage 相同的接口
|
||||
export const getItem = Storage.getItem.bind(Storage);
|
||||
export const setItem = Storage.setItem.bind(Storage);
|
||||
export const removeItem = Storage.removeItem.bind(Storage);
|
||||
export const getAllKeys = Storage.getAllKeys.bind(Storage);
|
||||
export const multiGet = Storage.multiGet.bind(Storage);
|
||||
export const multiSet = Storage.multiSet.bind(Storage);
|
||||
export const multiRemove = Storage.multiRemove.bind(Storage);
|
||||
export const clear = Storage.clear.bind(Storage);
|
||||
|
||||
// 同步方法(expo-sqlite/kv-store 的特色功能)
|
||||
export const getItemSync = Storage.getItemSync.bind(Storage);
|
||||
export const setItemSync = Storage.setItemSync.bind(Storage);
|
||||
export const removeItemSync = Storage.removeItemSync.bind(Storage);
|
||||
export const getAllKeysSync = Storage.getAllKeysSync.bind(Storage);
|
||||
export const clearSync = Storage.clearSync.bind(Storage);
|
||||
|
||||
// 默认导出,与 AsyncStorage 接口完全兼容
|
||||
export default Storage;
|
||||
@@ -1,4 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
|
||||
interface LogEntry {
|
||||
id: string;
|
||||
|
||||
@@ -670,7 +670,7 @@ export class WaterNotificationHelpers {
|
||||
|
||||
// 检查是否在过去2小时内已经发送过喝水提醒,避免重复打扰
|
||||
const lastNotificationKey = '@last_water_notification';
|
||||
const AsyncStorage = (await import('@react-native-async-storage/async-storage')).default;
|
||||
const AsyncStorage = (await import('@/utils/kvStore')).default;
|
||||
const lastNotificationTime = await AsyncStorage.getItem(lastNotificationKey);
|
||||
const now = new Date().getTime();
|
||||
const twoHoursAgo = now - (2 * 60 * 60 * 1000); // 2小时前
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
|
||||
// 用户偏好设置的存储键
|
||||
const PREFERENCES_KEYS = {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import { NativeModules } from 'react-native';
|
||||
|
||||
// Widget数据同步服务
|
||||
|
||||
Reference in New Issue
Block a user