1

I have some data saved in a shared preferences & on the firebase, in case the application data was deleted i need to fetch it from firebase and if it's doesn't exist in both shared preferences and firebase i return null.

The problem is that the method doesn't wait for firebase to return the data and returns null. I tried this solution and i return's null too, i guess because the return at the end of the function but when i removed it i got an "missing return statment" error.

Any idea how to fix it?

My original code:

public ArrayList<News> getFavorites() {
    SharedPreferences sharedPreferences;
    sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

    favoritesArrayList = new ArrayList<>();

    if (sharedPreferences.contains(FAVORITES)) {
        String json = sharedPreferences.getString(FAVORITES, null);
        Gson gson = new Gson();
        News[] newsArray = gson.fromJson(json, News[].class);
        favoritesArrayList = new ArrayList<News>(Arrays.asList(newsArray));
        Helper.displayLog("s", "shared pref:" + favoritesArrayList.size());
        return favoritesArrayList;
    } else {
        //check if in firebase
       Constants.favoritesDatabaseReference.child(Constants.currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                for(DataSnapshot ds:dataSnapshot.getChildren()){
                    favoritesArrayList.add(ds.getValue(News.class));
                }
            }
            else
                favoritesArrayList=null;
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    }
   return favoritesArrayList;
}

Code after trying the solution:

public ArrayList<News> getFavorites() {
        SharedPreferences sharedPreferences;
        sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

        favoritesArrayList = new ArrayList<>();

        if (sharedPreferences.contains(FAVORITES)) {
            String json = sharedPreferences.getString(FAVORITES, null);
            Gson gson = new Gson();
            News[] newsArray = gson.fromJson(json, News[].class);
            favoritesArrayList = new ArrayList<News>(Arrays.asList(newsArray));
            Helper.displayLog("s", "shared pref:" + favoritesArrayList.size());
            return favoritesArrayList;
        } else {
            //check if in firebase
            getNewsFromFirebase(new CallBack() {
                @Override
                public ArrayList<News> onCallback(ArrayList<News> newsArrayList) {
                    return favoritesArrayList=newsArrayList;
                }
            });

        }
        return favoritesArrayList;
    }

    private void getNewsFromFirebase(final CallBack callBack){
        Constants.favoritesDatabaseReference.child(Constants.currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    for(DataSnapshot ds:dataSnapshot.getChildren()){
                        favoritesArrayList.add(ds.getValue(News.class));
                    }
                    callBack.onCallback(favoritesArrayList);
                    Helper.displayLog(context.getClass().getSimpleName(),"SP class "+"list size: "+favoritesArrayList.size());
                }
                else
                    favoritesArrayList=null;
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private interface CallBack {
        ArrayList<News> onCallback(ArrayList<News> newsArrayList);
    }
Toka A. Amin
  • 156
  • 1
  • 2
  • 16
  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Jul 16 '18 at 11:35
  • @AlexMamo I edited the question with why it's not working and it's not a duplicate i need to do something slightly different and it's not working out for me as I mentioned. – Toka A. Amin Jul 16 '18 at 11:49
  • It's the exact same thing. You cannot return that list as a result of a method. I have also added another [duplicate](https://stackoverflow.com/questions/51355443/not-able-to-access-inner-class-data-from-outer-class-in-android/51359832). Please also take a look there. – Alex Mamo Jul 16 '18 at 11:51

0 Answers0