2017-10-09 1 views
5

Mit der Echtzeit-Datenbank zu konvertieren, man dies tun könnte:Firebase Firestor: Wie Dokumentobjekt zu einem POJO auf Android

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

als eine Möglichkeit, um das Objekt abzubilden, wie kann man dies mit 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()); 
      } 
     }); 

Antwort

3

Mit einer DocumentSnapshot Sie tun können:

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

werden diese Verschleppung der Dokument-ID? –

0

Nicht sicher, ob dies der beste Weg, es zu tun ist, aber das ist, was ich bisher haben.

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

EDIT: Ich verwende jetzt, was auf der angenommenen Antwort ist.

Verwandte Themen