1

I am trying to save a Firebase DataSnapshot object to sharedpreferences.

See this post in which I have devised a method that involves doing this.

...

What I have tried so far:

  1. Using Gson.Json method. RESULT: Doesn't seem to work... I don't think the DataSnapshot class is a "POJO" type class... at least not one that will work with gson.

  2. Using this method:

    private static String toString( Serializable o ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( o ); oos.close(); return Base64.getEncoder().encodeToString(baos.toByteArray()); }

RESULT: Doesn't work... I don't think that the DataSnapshot class is 'Serializable'.

I thought of another method -- Just save DataSnapshot.toString() into Sharedpreferences... but then how do you get it back out again? You can't do:

DataSnapshot snapshot = new DataSnapshot().fromString([string from sharedprefs])

Any help is welcome.

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • You're not supposed to save big chunks of **data** in sharedpreferences – Tim Sep 14 '16 at 09:46
  • How do you know how big it is, have you seen my database? How big is too big? – Nerdy Bunz Sep 14 '16 at 09:48
  • this is bad approach to store datasnapshot in shared preference and this is technically not phisible – Vishal Patoliya ツ Sep 14 '16 at 09:48
  • Regardless of the size of the data, my 'advice' still stands. I would suggest you look for an alternative – Tim Sep 14 '16 at 09:49
  • 1
    Firebase supports directly enablePersistenceStorage for offline support – Vishal Patoliya ツ Sep 14 '16 at 09:50
  • Well... if either of you could look deeper into the post I referred to that explains why I'm trying to do this, and suggest a better design I would be much obliged. – Nerdy Bunz Sep 14 '16 at 09:50
  • Thankyou Vishal... I'm going to study that next I think that might hold the solution. – Nerdy Bunz Sep 14 '16 at 09:56
  • You can make a model class acting as a adapter between DataSnapshot and then you can use Gson to save it. – Linxy Sep 14 '16 at 16:56
  • Check [this](http://stackoverflow.com/a/39435730/6561141). It might help. – mrtpk Sep 15 '16 at 06:53
  • That's the same as" "What I have tried so far: 1) Using Gson.Json method. RESULT: Doesn't seem to work... I don't think the DataSnapshot class is a "POJO" type class... at least not one that will work with gson." – Nerdy Bunz Sep 15 '16 at 08:07
  • Thanks though... I think I'm going to try a different approach anyway... looks like "callback hell" is my only choice considering my present knowledge and abilities. – Nerdy Bunz Sep 15 '16 at 08:08

1 Answers1

1

I was facing same issue. I wanted to save Datasnapshot inside SharedPreferences and my json data was bit complicated. So I wrote below code.

//To save to sharedpreference

 public void setAppLanguageDataSnapshot(DataSnapshot mDataSnapshot) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        String json = mapper.writeValueAsString(mDataSnapshot.getValue());
        StorageUtils.getInstance(context).setString(AppConstants.SharedPrefConstants.APP_CONTENT, json);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
   }

//To get data as JSON object.

 String data = StorageUtils.getInstance(context).getString(AppConstants.SharedPrefConstants.APP_CONTENT);
    String value = "";
    if (!TextUtils.isEmpty(data)) {
        try {
            JSONObject jsonObject = new JSONObject(data);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
andro-girl
  • 7,989
  • 22
  • 71
  • 94