如何在Android上正确阅读AsyncStorage [英] How to Read AsyncStorage on Android Correctly
问题描述
似乎可以从Android(本机)访问AsyncStorage
;但我仍在努力使之工作.
It seems that it's possible access the AsyncStorage
from Android (native); but I still struggling to make this work.
在我的React Native应用程序中,我有类似的东西:
In my React Native application, I have something like:
商店
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) {
...
}
}
从商店中获取用户
const user = store.getState().user
console.log(user.first_name) // Steve
...
现在,当我尝试从Android获取同一用户时,问题就开始了,这是我所有沮丧的尝试:
Now the issue starts when I try to get the same user from Android, of all my frustrated attempts this is what I have:
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'
的正确方法,所以我尝试获取'user'
但没有用.
Since the User it's an object, I don't know if this is the right way to get the 'first_name'
, I tried get 'user'
but didn't worked.
我开始认为我应该使用 react-native-sqlite-storage 我会在哪里知道我的数据结构,但我不知道我是否能够在Android上访问该数据库.
I'm starting to think that I should use react-native-sqlite-storage where I would know my data structure, but I don't know if I would be able to access that database on Android.
PS:我要访问AsyncStorage
而不是SharedPreferences
PS: I want access AsyncStorage
not SharedPreferences
库版本
react-native: 0.48.4
redux: 3.7.2
redux-persist: 5.4.0
一些无法解决的问题
- 从本机代码访问AsyncStorage
- 可以我可以从Java层访问存储在React Native的AsyncStorage中的数据吗?
- React Native:如何使用Java代码通过React Native异步存储获取数据
- 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对象,相反,我期望key
和value
的许多行;这就是为什么我变得空着的原因.
AsyncStorage
it's an unique JSON Object and instead I was expecting many rows of key
and value
; That's why I was getting null.
所以首先我继续查询
catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"key", "value"}, null, null, null, null, null);
然后我检查以避免空指针
then I check to avoid null pointer
if (catalystLocalStorage.moveToFirst()) {
然后我抓取
do {
// JSONObject will ask for try catch
JSONObject obj = new JSONObject(catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value")));
} while(catalystLocalStorage.moveToNext());
我敢肯定,这样做可能会有更好的方法,但是我知道我可以接受.
I'm sure that may have better ways to do this, but right know I'm fine with that.
如果您有更好的主意,请离开您的思想.
If you have better ideas please leave your thought.
更新
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!