MyApplication purpose: Activity class that 1) Reads firebase db, 2) Updates class member variables based on data what was read from firebase db, 3) Evaluate credentials based on class member variables that were updated from the values inside the firebase db
public MyApplication extends AppCompatActivity implements FireBaseDataListener{
private static int N; /* number of firebase keys *//* static to be accessed in db read */
private Button Nbtn;
private FirebaseDataBase mFirebaseDataBase;
private FirebaseReference mFirebaseReference;
... instantiated class member variables in @OnCreate...
... create setters and getters and add listeners ...
public void processing(){
/* here is the issue, normally i would think this.N updates
before checkCredentials is called. wrong! since the asynchronous
design of firebase db i'm perplexed what to do in order to get
this.N to update before checkCredentials() is called.
due to this in checkCredentials i get N==0 not N>0.
however, i peek at the log.d printout and see the value updates
so i trace the program and see checkCredentials evaluates
way before readNfromFireBase is done. */
Log.d.("...", "before firebase read");
readNfromFireBase(this);
Log.d.("...", "before checkCredentials");
checkCredentials();
}
public void checkCredentials(){
Log.d.("...", "inside checkCredentials");
if(this.N == 0) { empty firebase launches dialog warning no keys }
elseif(this.N > 0) { Log.d("log#1", "N: " + N); }
else { Log.d("log#0", "N: " + N + " this should never happen but just in case.") }
}
public void readNfromFireBase(FireBaseDataListener listener){
listener.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
... for each loop to iterate over the data
/* before sending data to callback i print and verify the number
of keys is correct, so i know i have the correct data */
Log.d.("...", "before listener");
this.N = listener.onKeyCallback(data); // WARNING#0 this.N (i'm aware)
Log.d.("...", "after listener");
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
}
/* Override for FireBaseDataListener */
@Override public onKeyCallBack(int nKeys){
Log.d.("...", "inside callback listener");
this.N = nKeys; // WARNING#1 this.N (i'm aware)
return this.N;
}
} //end of my application class
interface FireBaseDataListener{
void onKeyCallback(int nKeys);
} // end of interface
notes on WARNING#0 & WARNING#1 - for learning purpose can someone shed some light which is more appropriate, to update the value inside the listener routine or inside the override callback, in other words, where should i update, inside warning#0 or warning#1?
sample execution - weird but it goes like this
"before firebase read"
"before listener"
"before checkCredentials"
"inside checkCredentials"
"inside callback listener"
"after listener"
sample execution - this is how it should be, but it's not.
"before firebase read"
"before listener"
"inside callback listener"
"after listener"
"before checkCredentials"
"inside checkCredentials"
In short, the code is evaluating future functions before the database read returns.