Created
July 25, 2016 19:04
-
-
Save Caerbannog/a68c75979f4e9c92b60497bad01ebdc9 to your computer and use it in GitHub Desktop.
Google Firebase Database, serialize and unserialize a ServerValue.TIMESTAMP with the same Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```java | |
// Model | |
public class User { | |
public Object timestamp; // Server time. | |
... | |
} | |
// Example code | |
public void test() { | |
// Save | |
user.timestamp = ServerValue.TIMESTAMP; | |
FirebaseDatabase...setValue(user); | |
// Load | |
FirebaseDatabase...addListenerForSingleValueEvent(DataSnapshot dataSnapshot) { | |
User user = dataSnapshot.getValue(User.class); | |
... | |
if (user.timestamp instanceof Long) { | |
Timber.w("read_value = %s", (Long) user.timestamp); | |
} | |
else { | |
Timber.e("Unexpected timestamp type for %s", user.timestamp); | |
} | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment