I'm new to Cloud Firestore and trying to figure things out. I'm currently trying to create a simple database for Android that contains the ID of a user and the time they installed the app so I can know if they passed their trial period. Everything seems very simple except getting the date and time (which I want to get from the server). People are saying I should use:
FieldValue.serverTimestamp()
But others say that doesn't work for adding a user. Also, this method returns something called "FieldValue", what is that, and can it be used to compare the user's date and time with the current date and time?
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
FirebaseFirestore db = FirebaseFirestore.getInstance();
// Create a new user with an ID and time
Map<String, Object> user = new HashMap<>();
user.put("userID", deviceId);
user.put("timeStamp", FieldValue.serverTimestamp());
// Add a new document with a generated ID
db.collection("users")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d("document", "DocumentSnapshot added with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("document", "Error adding document", e);
}
});