1

So I am trying to pass objects between activities using the onSharedPreferenceChanged in Shared Preferences:

In one activity class I call the prefs like this:

 mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
 mPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
            getData();
        }
    });

public void getData(){
    Gson gson = new Gson();
    String json = mPrefs.getString("ListAnimals", "");
    ArrayList<SpottedAnimal> animalList = gson.fromJson(json, ArrayList.class);
}

fireClient = new FirebaseClient(context:this, mPrefs);

And then on the FirebaseClient I call a singleValueEventListener to get all the data from the DB.

 Query query = mDatabase.orderByChild("id");
 query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot ds :dataSnapshot.getChildren()){
                 final  DataSnapshot dataSnap = ds;
                 String id = dataSnap.child("id").getValue(String.class); 
                 mStorageRef.child("picture_Thumb"+id).
                 getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                             SpottedAnimal pet = dataSnap.getValue(SpottedAnimal.class);
                             pet.setPictureIDThumb(uri.toString());
                             animalList.add(pet);
                             saveList();
                }
        }

 public void saveList(){
    String listJson = gson.toJson(animalList);
    prefsEditor.putString("ListAnimals", listJson);
    prefsEditor.commit();
}

For now this is mostly a test, I'm going to be using onChildEvent but still when all the data gets pulled from the server it should at least trigger onSharedPreferenceChanged a few times.

Also on the FirebaseClient constructor I get the Prefs from the activity class:

    this.mPrefs=mPrefs;
    prefsEditor = mPrefs.edit();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
BryceSoker
  • 624
  • 1
  • 11
  • 29

1 Answers1

0

You cannot use something now, that hasn't been loaded yet. With other words, you cannot simply create the animalList as a global variable and use it outside the onDataChange() method because it will always be empty due the asynchronous behaviour of this method. This means that by the time you are trying to use that list outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use that list only inside the onDataChange() method, otherwise I recommend you see the last part of my anwser from this post in which I have exaplined how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yes but that is why the saveList() is inside onDataChange(), so it only supposedly "triggers" the commit only after the object is added to it. And supposedly onSharedPreferenceChanged is only called AFTER the commit. – BryceSoker May 08 '18 at 20:50
  • Try do define the method like this: `public void saveList(ArrayList animalList)` and then call it from `onDataChange()` like this: `saveList(animalList);`. Does it work now? – Alex Mamo May 08 '18 at 20:57
  • Nope didn't work, still doesn't get triggered. The objects do get commited since I tried checking them with logs on the FirebaseClient side but nothing comes out on the other side. – BryceSoker May 08 '18 at 20:59