如何从Firestore获取当前日期和时间并将其保存? [英] How to get the current date and time from Firestore and save it?
问题描述
我是Cloud Firestore的新手,正在尝试解决问题.我目前正在尝试为Android创建一个简单的数据库,其中包含用户的ID和用户安装该应用程序的时间,这样我就可以知道他们是否通过了试用期.除了获取日期和时间(我想从服务器获取日期和时间)之外,其他一切看起来都非常简单.人们说我应该使用:
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()
但是其他人说这对添加用户无效.另外,此方法返回一个称为"FieldValue"的东西,它可以用来将用户的日期和时间与当前日期和时间进行比较吗?
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);
}
});
推荐答案
首先,使用"deviceId"作为Firestore中用户的唯一标识符,在我看来并不是一个好主意.主要原因是用户可以在任何时间点更改设备,这意味着"users/$ deviceId/"下的所有数据都可以被替换.会迷路.因此,在这种情况下,最好的方法是使用Firebase对用户进行身份验证并用作唯一标识符,来自身份验证过程的UID.
First of all, using the "deviceId" as a unique identifier for your users in Firestore doesn't seem to me like a good idea. The main reason is that a user can change the device at any point in time, meaning that all the data under "users/$deviceId/" will be lost. So the best approach, in this case, is to authenticate your users with Firebase and use as a unique identifier the UID that comes from the authentication process.
要将用户对象写入数据库,您只需使用以下代码行即可:
To write a user object to the database, you can simply use the following lines of code:
Map<String, Object> user = new HashMap<>();
user.put("userID", deviceId);
user.put("timeStamp", FieldValue.serverTimestamp());
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
CollectionReference usersRef = rootRef.collection("users");
usersRef.document(uid).set(user).addOnSuccessListener(/* ... */);
现在,要读回"timeStamp"的值,则将其重新设置为0.属性,则需要使用"get()"调用,如以下代码行所述:
Now, to read back the value of the "timeStamp" property, you need to use a "get()" call, as explained in the following lines of code:
usersRef.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Date timeStamp = document.getDate("timeStamp");
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
具有"timeStamp";对象,现在您可以获取当前日期和时间并根据需要在项目中进行比较.
Having the "timeStamp" object, now you can get the current date and time and compare them as needed in your project.
这篇关于如何从Firestore获取当前日期和时间并将其保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!