You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android端正确读取React Native的AsyncStorage

在Android原生端访问React Native redux-persist持久化的AsyncStorage数据问题

我最近遇到个棘手的问题:想从Android原生端访问React Native里用redux-persist持久化到AsyncStorage的用户数据,但试了好多次都没成功。先给大家说说我的环境和代码情况:

React Native端的配置与代码

我的RN应用用redux-persist做数据持久化,相关代码如下:

Store配置代码

const config = { ... }
export const reducers = { ... user: user }
let rootReducer = combineReducers(reducers)
let reducer = persistReducer(config, rootReducer)

User Reducer代码

export class Actions { ... }
const initialState = { first_name: null, last_name: null, email: null, addresses: [ { ... } ] }
export const reducer = (state = initialState, action) => { switch (action.type) { ... } }

RN中获取用户数据的代码

在RN里我能正常从store拿到用户数据:

const user = store.getState().user
console.log(user.first_name) // Steve

Android原生端的尝试代码

我尝试用以下原生代码去读取AsyncStorage,但始终失败:

try {
readableDatabase = ReactDatabaseSupplier.getInstance(getReactApplicationContext()).getReadableDatabase();
catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"value"}, "key = ?", new String[] { "full_name" }, null, null, null);
String[] names = catalystLocalStorage.getColumnNames();
if (catalystLocalStorage.moveToFirst()) {
final String value = catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value"));
Log.d(TAG, value);
}
} finally {
if (catalystLocalStorage != null) { catalystLocalStorage.close(); }
if (readableDatabase != null) { readableDatabase.close(); }
}

遇到的困惑

  • 因为User是一个对象,我不确定该怎么正确获取first_name这个字段,尝试查询user这个键也没成功
  • 我考虑过改用react-native-sqlite-storage库,但不确定原生端能不能访问这个库创建的数据库
  • 特别说明:我需要访问的是AsyncStorage,而不是SharedPreferences

使用的库版本

  • react-native: 0.48.4
  • redux: 3.7.2
  • redux-persist: 5.4.0

我之前查过几个相关问题,但都没解决我的问题。

内容的提问来源于stack exchange,提问作者Pablo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:42:11