0

I have a Firebase Database that I use to fill a ListView with FirebaseUI. Everything works fine (offline and online) until the app is killed and restarted without internet. When this happens, the database is empty and once the internet is back everything it synchronizes perfectl.

I use:

    Firebase.getDefaultConfig().setPersistenceEnabled(true);

but it doesn't change anything. What should I do to in order to have the offline Db even after killing the app?

    database = FirebaseDatabase.getInstance(FirebaseApp.getInstance());
    ref = database.getReference(FirebaseAuth.getInstance(FirebaseApp.getInstance()).getCurrentUser().getUid())
            .child("Tasks");
    Log.d(TAG,"User ID: " +FirebaseAuth.getInstance(FirebaseApp.getInstance()).getCurrentUser().getUid());
    Log.d(TAG,"DB: " +database);
    Log.d(TAG,"Ref: " +ref);
    mAdapter = new FirebaseListAdapter<Task>(getActivity(), Task.class, R.layout.task_view,ref) {
        @Override
        protected void populateView(View v, Task model, int position) {
            TextView task = (TextView) v.findViewById(R.id.task_text);
            CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbox); 
            task.setText(model.getText());
            checkBox.setChecked(model.getDone()==1);
        }
    };
    listView.setAdapter(mAdapter);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Thirsty
  • 188
  • 2
  • 12

2 Answers2

0

Instead of Firebase.getDefaultConfig().setPersistenceEnabled(true); try setting this in your Application Class FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Check out this link. if you get errors

Community
  • 1
  • 1
Siddhesh Dighe
  • 2,894
  • 3
  • 16
  • 16
0

You Just need to create a class like this

public class MyFirebaseApp extends android.app.Application {

    @Override
    public void onCreate() {
        super.onCreate();
/* Enable disk persistence  */
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }

}

and define this line in manifest file. android:name="package_name.MyFirebaseApp">

saurabh dixit
  • 863
  • 6
  • 19