My app is working normally but he crash if i logout and try to login again. After the crash if i try to open the app again he open normally.
This is the error:
com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
This is my classe:
DatabaseReference databaseReference;
RecyclerView recyclerView;
ArrayList<Tasks> tasksArrayList;
UsersRecyclerAdapter adapter;
GoogleSignInOptions gso;
GoogleSignInClient gsc;
String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar());
CheckLogin();
FirebaseDatabase.getInstance().setPersistenceEnabled(true); // work offline
databaseReference = FirebaseDatabase.getInstance().getReference();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
tasksArrayList = new ArrayList<>();
ReadData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.add) {
ViewDialogAdd viewDialogAdd = new ViewDialogAdd();
viewDialogAdd.showDialog(MainActivity.this);
}else if (id == R.id.logout) {
gsc.signOut().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
finish();
}
});
}
return super.onOptionsItemSelected(item);
}
private void CheckLogin(){
//Check if user is logged in
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
gsc = GoogleSignIn.getClient(this, gso);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null){
userID = account.getId();
} else {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}
private void ReadData() {
if (userID != null) {
databaseReference.child("DB").child(userID).child("TASKS").orderByChild("id").addValueEventListener(new ValueEventListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
tasksArrayList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Tasks users = dataSnapshot.getValue(Tasks.class);
tasksArrayList.add(users);
}
adapter = new UsersRecyclerAdapter(MainActivity.this, tasksArrayList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
public class ViewDialogAdd {
public void showDialog(Context context) {
/*Body*/
}
}
I tried to change this line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
I put it in different places in my code but the error is the same. I also tried to remove the CheckLogin(); but the error persist the same way
CodePudding user response:
Yesterday i make a few researches about my error and found nothing. Today i made i few more searches and this here helped me fix the problem.