How to Read AsyncStorage on Android Correctly

前端 未结 2 1141
死守一世寂寞
死守一世寂寞 2020-12-06 23:17

It seems that it\'s possible access the AsyncStorage from Android (native); but I still struggling to make this work.

In my React Native application, I

相关标签:
2条回答
  • 2020-12-06 23:33

    I had a problem with alarm clock in my application and I decided to use AsyncStorage on Android side, then I use the code below and I could get all values from AsyncStorage and generate all alarms that was registered in my application's AsyncStorage.

    SQLiteDatabase readableDatabase = null;
            readableDatabase = ReactDatabaseSupplier.getInstance(this.reactContext).getReadableDatabase();
            Cursor catalystLocalStorage = readableDatabase.query("catalystLocalStorage", null, null, null, null, null, null);
            try {
                List<AlarmeDTO> listaAlarmes = new ArrayList<>();
                if (catalystLocalStorage.moveToFirst()) {
    
                    do {
                        //Ex: key: 01082019_0800
                        //value: [{executionDate: 2019-08-01T08:00}]
                        String key = catalystLocalStorage.getString(0);
                        String json = catalystLocalStorage.getString(1);
    
                        if (dados.length == 3) {
                            ...generate my alarms with key and json
                        }
    
                    } while (catalystLocalStorage.moveToNext());
    
                }//2_31082019_0900 - [{"idPrescricaoMedicamento":35,"nomeMedicamento":"VITAMINA"}]
    
            } finally {
                if (catalystLocalStorage != null) {
                    catalystLocalStorage.close();
                }
    
                if (readableDatabase != null) {
                    readableDatabase.close();
                }
            }
    
    0 讨论(0)
  • 2020-12-06 23:54

    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.

    So first I continue with the query

    catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"key", "value"}, null, null, null, null, null);
    

    then I check to avoid null pointer

    if (catalystLocalStorage.moveToFirst()) {
    

    then I do the fetch

    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.

    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 "";
            }
        }
    }
    

    Calling the class

    AsyncStorage as = new AsyncStorage(context);
    as.getFullname()
    
    0 讨论(0)
提交回复
热议问题