Firebase Firestore:如何在 Android 上将文档对象转换为 POJO [英] Firebase Firestore : How to convert document object to a POJO on Android
本文介绍了Firebase Firestore:如何在 Android 上将文档对象转换为 POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用实时数据库,可以做到这一点:
With the Realtime Database, one could do this :
MyPojo pojo = dataSnapshot.getValue(MyPojo.Class);
作为映射对象的一种方式,如何使用 Firestore
做到这一点?
as a way to map the object, how does one do this with Firestore
?
代码:
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("app/users/" + uid).document("notifications").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
NotifPojo notifPojo = document....// here
return;
}
} else {
Log.d("FragNotif", "get failed with ", task.getException());
}
});
推荐答案
使用 DocumentSnapshot
,您可以:
DocumentSnapshot document = future.get();
if (document.exists()) {
// convert document to POJO
NotifPojo notifPojo = document.toObject(NotifPojo.class);
}
这篇关于Firebase Firestore:如何在 Android 上将文档对象转换为 POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文