
似乎可以从Android(本机)访问AsyncStorage;但我仍然在努力做这项工作.
在我的React Native应用程序中,我有类似的东西:
商店
const config = { ...}export const reducers = { ... user: user}let rootReducer = combineReducers(reducers)let reducer = persistReducer(config, rootReducer)用户减速机
export class Actions { ...}const initialState = { first_name: : null, last_name: : null, email: null, addresses: [ { ... } ] } }}export const reducer = (state = initialState, action) => { switch (action.type) { ... }}从商店获取用户
const user = store.getState().userconsole.log(user.first_name) // Steve...现在,当我尝试从AndroID获得相同的用户时,问题开始了,我所有的挫败尝试都是这样:
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(); }}由于用户是一个对象,我不知道这是否是获取’first_name’的正确方法,我尝试得到’用户’但没有用.
我开始认为我应该使用react-native-sqlite-storage来了解我的数据结构,但我不知道我是否能够在AndroID上访问该数据库.
PS:我想访问AsyncStorage而不是SharedPreferences
lib版本
react-native: 0.48.4redux: 3.7.2redux-persist: 5.4.0有些问题不起作用
> Access AsyncStorage from native code
> Can I access data stored in React Native’s AsyncStorage from java layer?
> React Native: How to use Java code to fetch data via React Native async storage
解决方法:
AsyncStorage它是一个独特的JsON对象,而我期待的是许多行的键和值;这就是为什么我变空了.
所以首先我继续查询
catalystlocalstorage = readableDatabase.query("catalystlocalstorage", new String[]{"key", "value"}, null, null, null, null, null);然后我检查以避免空指针
if (catalystlocalstorage.movetoFirst()) {然后我做了取
do { // JsONObject will ask for try catch JsONObject obj = new JsONObject(catalystlocalstorage.getString(catalystlocalstorage.getColumnIndex("value")));} while(catalystlocalstorage.movetoNext());我相信可能有更好的方法来做到这一点,但是对,我知道我很好.
如果你有更好的想法,请留下你的想法.
UPDATE
import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.util.Log;import com.facebook.react.brIDge.ReactApplicationContext;import com.facebook.react.brIDge.ReactContextBaseJavaModule;import com.facebook.react.modules.storage.ReactDatabasesupplier;import org.Json.JsONArray;import org.Json.JsONObject;import java.util.ArrayList;public class AsyncStorage { public static String TAG = "RNAsyncStorage"; public ReactApplicationContext context; public ArrayList<JsONObject> collection; public JsONObject data; public JsONObject user; Cursor catalystlocalstorage = null; sqliteDatabase readableDatabase = null; public AsyncStorage (ReactApplicationContext context) { this.context = context; this.collection = new ArrayList<JsONObject>(); this.fetch(); this.setUser(); } public voID fetch() { try { readableDatabase = ReactDatabasesupplier.getInstance(context).getReadableDatabase(); catalystlocalstorage = readableDatabase.query("catalystlocalstorage", new String[]{"key", "value"}, null, null, null, null, null); if (catalystlocalstorage.movetoFirst()) { do { try { // one row with all AsyncStorage: { "user": { ... }, ... } String Json = catalystlocalstorage.getString(catalystlocalstorage.getColumnIndex("value")); JsONObject obj = new JsONObject(Json); String user = obj.getString("user"); JsONObject res = new JsONObject(); res.put("user", new JsONObject(user)); collection.add(res); } catch(Exception e) { // do something } } while(catalystlocalstorage.movetoNext()); } } finally { if (catalystlocalstorage != null) { catalystlocalstorage.close(); } if (readableDatabase != null) { readableDatabase.close(); } data = this.collection.get(0); } } public String getFullname () { try { return user.getString("fullname"); } catch (Exception e) { return ""; } }}打电话给上课
AsyncStorage as = new AsyncStorage(context);as.getFullname() 总结 以上是内存溢出为你收集整理的如何正确读取Android上的AsyncStorage全部内容,希望文章能够帮你解决如何正确读取Android上的AsyncStorage所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)