2017-10-09 157 views
5

與實時數據庫,一個可以這樣做:火力地堡公司的FireStore:如何文檔對象轉換爲一個POJO在Android

MyPojo pojo = dataSnapshot.getValue(MyPojo.Class); 

,以此來映射對象,一個人如何與Firestore做到這一點?

CODE:

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()); 
      } 
     }); 

回答

3

隨着DocumentSnapshot,你可以這樣做:

DocumentSnapshot document = future.get(); 
if (document.exists()) { 
    // convert document to POJO 
    NotifPojo notifPojo = document.toObject(NotifPojo.class); 
} 
請問
+0

這結轉文件ID? –

0

不知道這是做的最好的方式,但是這是我到目前爲止所。

NotifPojo notifPojo = new Gson().fromJson(document.getData().toString(), NotifPojo.class); 

編輯:我現在使用什麼是接受的答案。