I'd like to emulate the following, to exclude initial data from my Firebase listener, but do it in Java.
var ref = new Firebase('https://<your instance>.firebaseio.com/messages');
var queryRef = ref.orderBy('created').startAt(Firebase.ServerValue.TIMESTAMP);
queryRef.on('child_added', function(snap) {
console.log(snap.val());
});
There doesn't seem to be a signature of .startAt() that would take ServerValue.TIMESTAMP (which is a Map in the Java SDK) as a parameter. Is this query (using the Firebase server timestamp) simply not possible in Java?
The API doc says startAt() can take String, String, String, boolean, boolean, String, double, or double, String. So no. You can't pass a Map containing TIMESTAMP.
You could achieve a similar result by using .info/serverTimeOffset
Firebase offsetRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/serverTimeOffset");
offsetRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
double offset = snapshot.getValue(Double.class);
}
@Override
public void onCancelled(FirebaseError error) {
System.err.println("Listener was cancelled");
}
});
And now you can use that in your query:
double serverTime = System.currentTimeMillis() + offset;
ref.orderBy('createdAt').startAt(serverTime);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With