Reputation: 11
I have this code to read data and write the value into an object
if(SpecialFunction.isNetworkAvailable()) {
databaseContent.loadAccountFromDatabase(account -> {
this.account = account;
binding.textView3.setText(account.toString());
});
} else {
startActivity(new Intent(this, InternetTroubleActivity.class));
}
Where databaseContent is a class which contains Firebase logic which I need. In loadAccountFromDatabase I have next code which works with one problem.
public void loadAccountFromDatabase(FirebaseCallbackAccount accountFirebaseCallback) {
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
account = snapshot.getValue(Account.class);
} else {
account = new Account();
setDefaultAccount();
}
accountFirebaseCallback.onCallback(account);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.e("loadAccountFromDatabase", "Error: " + error.toString());
}
};
database.addValueEventListener(valueEventListener);
}
When I try to read data after internet reconnection or when I turn on internet after onStart() and run loadAccountFromDatabase I get null value of my snapshot. I have a method (isNetworkAvailable()) which works well and when I don't have internet connection it returns false. The next part of code doesn't work properly. The snapshot.exists returns true and snapshot.getValue returns null although the value is other.
if (snapshot.exists()) {
account = snapshot.getValue(Account.class);
}
If I run an application with internet connection everything works well while I don't turn off internet and try to read again. I also tried to use database.get().addOnCompleteListener(onCompleteListener)... but I got the same result. Everything works well while I don't try to read data after internet reconnection.
database.keepSynced(true)
also doesn't help.
Database init:
private final String USER_KEY = "Account", PURCHASES = "purchases";
public void init() {
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
database = firebaseDatabase.getReference(String.format("%s/%s", USER_KEY, mAuth.getUid()));
}
Structure: console
To help you understand the problem I add screenshot of log. GREEN box: I run my app with internet connection. The data was read and wrote correct. RED box: I closed my app, destroyed activity, turned off internet. Then I run my app, but method isNetworkAvailable didn't let me run MainActivity. Then I turned on internet and clicked on button. The data was being tried to read but I got object with null variables althrough they are not null. If you think that something is wrong with isNetworkAvailable I tell you that this problem was before I have added it. I was hoping that it would help me to read correct but it didn't help. logcat
JSON:
{
"Account" : {
"wBOZsnGGywYIpap3cLZodPOWcpt2" : {
"budget" : 100,
"budgetLastMonth" : 0,
"budgetLeft" : 100,
"currencyType" : "USD",
"email" : "[email protected]",
"id" : "wBOZsnGGywYIpap3cLZodPOWcpt2",
"personName" : "новый пользователь"
}
}
}
Upvotes: 0
Views: 57
Reputation: 11
I found a problem. 4 hours of reading my code and stackoverflow. I don't know how, but in onPause () I had code that writes null object to a database. So when I tested and turned on / off the Internet, I run it. So I wrote a null object that was not instantly updated on the console, and when I turned on the Internet, the object was updated to zero in the database and then read in my app. :]
Upvotes: 1