zcb
zcb

Reputation: 136

How to store Firebase server timestamp plus 30 seconds?

I'm storing objects on Firebase Realtime Database. An object looks like this:

import com.google.firebase.database.ServerValue;

public class Message {

    private Object timestamp = ServerValue.TIMESTAMP;
    private String source = "client";
    private String req;
    private String ack = null;
    private int pos;

    Message(){};    // needed for Firebase when converting DataSnapshot to Message object

    Message(String req, int pos) {
        this.req = req;
        this.pos = pos;
    }

    public Object getTimestamp() {
        return timestamp;
    }

    public String getSource() {
        return source;
    }

    public String getReq() {
        return req;
    }

    public String getAck() {
        return ack;
    }

    public int getPos() {
        return pos;
    }

}

I haven't tried it yet, though the timestamp should correctly be populated when storing the object onto Firebase.

Now my question is: how can I add an expiration attribute? The expiration would be equal to the TIMESTAMP plus 30 seconds like so:

private Object expiration = ServerValue.TIMESTAMP + <30 seconds>;

Upvotes: 3

Views: 151

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317657

Unfortunately, that's not possible. The only supported use of ServerValue.TIMESTAMP is to store the exact value of the server's clock in that child. It's a token value and not an exact number. The exact number isn't known until Firebase receives the token and translates it into a number.

Your only workarounds are to either use a backend to perform the write, or have the client read the value after it's written, and add 30s to it, and write it back.

Upvotes: 1

Related Questions