如何在Android上正确阅读AsyncStorage

Pab*_*blo 2 android react-native redux asyncstorage redux-persist

似乎可以AsyncStorage从Android(本机)访问;但我仍在努力使这项工作。

在我的React Native应用程序中,我有类似以下内容:

商店

const config = {
  ...
}

export const reducers = {
  ...
  user: user
}


let rootReducer = combineReducers(reducers)
let reducer = persistReducer(config, rootReducer)
Run Code Online (Sandbox Code Playgroud)

用户减速器

export class Actions {
    ...
}

const initialState = {
      first_name: : null,
      last_name: : null,
      email: null,
      addresses: [
          { ... }
      ]
    }
  }
}

export const reducer = (state = initialState, action) => {
  switch (action.type) {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

从商店获取用户

const user = store.getState().user
console.log(user.first_name) // Steve
...
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试从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();
    }
}
Run Code Online (Sandbox Code Playgroud)

由于User是一个对象,因此我不知道这是否是获取的正确方法'first_name',我尝试了get 'user'但没有用。

我开始认为应该在我会知道我的数据结构的地方使用react-native-sqlite-storage,但是我不知道我是否能够在Android上访问该数据库。

PS:我想获得AsyncStorageSharedPreferences


库版本

react-native: 0.48.4
redux: 3.7.2
redux-persist: 5.4.0
Run Code Online (Sandbox Code Playgroud)

一些不起作用的问题

Pab*_*blo 6

AsyncStorage这是一个唯一的JSON对象,相反,我期望keyand的很多行value。这就是为什么我越来越空。

所以首先我继续查询

catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"key", "value"}, null, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)

然后我检查以避免空指针

if (catalystLocalStorage.moveToFirst()) {
Run Code Online (Sandbox Code Playgroud)

然后我去取

do {
    // JSONObject will ask for try catch
    JSONObject obj = new JSONObject(catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value")));
} while(catalystLocalStorage.moveToNext());
Run Code Online (Sandbox Code Playgroud)

我敢肯定,这样做可能会有更好的方法,但是正确的方法是,我对此表示满意。

如果您有更好的想法,请离开您的想法。

更新

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 "";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

叫课

AsyncStorage as = new AsyncStorage(context);
as.getFullname()
Run Code Online (Sandbox Code Playgroud)