albanx
albanx

Reputation: 6335

Firebase cannot get the last insert value

I have a small firebase database with a list. How can I get the last inserted value of the list? The elements of the list have as key a date string, example:

 2016_05_29 --> data
 2016_05_30 --> data
 2016_05_31
 2016_06_01
 2016_06_02
 2016_06_03
 2016_06_10
 2016_06_11
 2016_06_12
 2016_06_13
 2016_06_14
 2016_06_15
 2016_06_16
 2016_06_17
 2016_06_18
 2016_06_19 //returned value
 2016_06_20
 2016_06_21
 2016_06_22
 2016_06_23 //expected value

I am try to get the last value of this list, so 2016_06_23 but for some reason it returns this 2016_06_19 whatever key/order system I use:

Query dateMaxRef = ref.orderByKey().limitToLast(1); //this returns always 2016_06_19 instead of 2016_06_23 
dateMaxRef.addListenerForSingleValueEvent(new ValueEventListener() 

....

Any idea?

Upvotes: 2

Views: 2519

Answers (1)

Tushar Nallan
Tushar Nallan

Reputation: 784

I faced the same issue and for me the reason was because the query was not using the latest data from firebase and instead relying on internal cache. Using .keepSynced(true) on the query fixed it for me. Below is the sample code -

Query causeListPath = mFirebase.child(PATH_CAUSE_LIST).child(SELECTED_CITY)
        .child(SELECTED_COURT_CODE).limitToLast(1);

// If user wants un-cached data, force sync
if (!useCache) causeListPath.keepSynced(true);

causeListPath.addChildEventListener(new ChildEventListener() {
...
});

Upvotes: 4

Related Questions