使用 toObject 方法将 Snapshot 转换为 POJO 时,将包含 Date 对象的代码转换为 Firebase.Timestamp 对象的最佳方法
Best way to convert code containing Date objects to Firebase.Timestamp objects when using toObject method to convert Snapshot to POJOs
我注意到我收到以下 警告,每次我 运行 我的应用程序代码:
W/Firestore: (0.6.6-dev) [Firestore]: The behavior for java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
firestore.setFirestoreSettings(settings);
With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp. For example:
// Old:
java.util.Date date = snapshot.getDate("created_at");
// New:
Timestamp timestamp = snapshot.getTimestamp("created_at");
java.util.Date date = timestamp.toDate();
Please audit all existing usages of java.util.Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
如果您从 DocumentSnapshot 中单独获取每个文档对象,则警告提供的示例建议转换代码的最佳方法。
我的问题 这里是 - 如果您在 DocumentSnapshot 上使用 .toObject()
方法直接从中获取 POJO 怎么办,有没有 suggested/optimized 方法在这种情况下更新 code/data 模型?
由于以下场景java.util.Date
目前已在应用中使用-
- 由于在整个应用程序中有很多地方使用了日期对象。
- 此外,日期是一些地图数据类型对象的值(例如
Map<String, Date>
)。 documentSnapshot中这种类型的数据对象,如何在没有太大改动的情况下进行代码转换?
One way I have already thought is to update the POJO's with java.util.Date
fields to com.google.firebase.Timestamp
as well as in the Map<K, V>
implementations. After that update the code implementation by adding .toDate()
after the Timestamp field calls. But this way doesn't seem optimized.
当使用 toObject() 时,Firestore SDK 会自动将时间戳文档字段转换为 POJO class 成员的日期对象。它能够在运行时检测类型以进行转换。
您引用的警告是针对在直接访问快照数据时尝试假定对象类型的代码。
我注意到我收到以下 警告,每次我 运行 我的应用程序代码:
W/Firestore: (0.6.6-dev) [Firestore]: The behavior for java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
firestore.setFirestoreSettings(settings);
With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp. For example:
// Old:
java.util.Date date = snapshot.getDate("created_at");
// New:
Timestamp timestamp = snapshot.getTimestamp("created_at");
java.util.Date date = timestamp.toDate();
Please audit all existing usages of java.util.Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
如果您从 DocumentSnapshot 中单独获取每个文档对象,则警告提供的示例建议转换代码的最佳方法。
我的问题 这里是 - 如果您在 DocumentSnapshot 上使用 .toObject()
方法直接从中获取 POJO 怎么办,有没有 suggested/optimized 方法在这种情况下更新 code/data 模型?
由于以下场景java.util.Date
目前已在应用中使用-
- 由于在整个应用程序中有很多地方使用了日期对象。
- 此外,日期是一些地图数据类型对象的值(例如
Map<String, Date>
)。 documentSnapshot中这种类型的数据对象,如何在没有太大改动的情况下进行代码转换?
One way I have already thought is to update the POJO's with
java.util.Date
fields tocom.google.firebase.Timestamp
as well as in theMap<K, V>
implementations. After that update the code implementation by adding.toDate()
after the Timestamp field calls. But this way doesn't seem optimized.
当使用 toObject() 时,Firestore SDK 会自动将时间戳文档字段转换为 POJO class 成员的日期对象。它能够在运行时检测类型以进行转换。
您引用的警告是针对在直接访问快照数据时尝试假定对象类型的代码。