了解 dataSnapshot 对象中的数据

注意: 在完全理解此示例之前 你需要先了解 getReference() 引用的数据

从 Firebase 实时数据库获取数据有三种常用方法:

  • addValueEventListener()
  • addListenerForSingleValueEvent()
  • addChildEventListener()

当我们谈论 dataSnapshot 对象中的哪些数据时,addValueEventListener()addListenerForSingleValueEvent() 基本相同。唯一的区别是 addValueEventListener() 继续听取引用数据中所做的更改,而 addListenerForSingleValueEvent() 则没有。

所以考虑我们有这个数据库:

"your-project-name" : {
    "users" : {
        "randomUserId1" : {
            "display-name" : "John Doe",
            "gender" : "male"
        }
        "randomUserId2" : {
            "display-name" : "Jane Dae",
            "gender" : "female"
        }
    },
    "books" {
        "bookId1" : {
            "title" : "Adventure of Someone"
        },
        "bookId1" : {
            "title" : "Harry Potter"
        },
        "bookId1" : {
            "title" : "Game of Throne"
        }
    }
}

DataSnapshot 由 addValueEventListener 和 addListenerForSingleValueEvent 生成

addValueEventListener()addListenerForSingleValueEvent() 生成的 dataSnapshot 将包含它被引用的确切数据的值。就像当 ref 指向 your-project-name 然后 dataSnapshot 应该是:

... onDataChange(DataSnapshot dataSnapshot) {
    dataSnapshot.getKey(); // will have value of String: "your-project-name"
    for (DataSnapshot snapshot : dataSnapshot) {
        snapshot.getKey(); // will have value of String: "users", then "books"
        for (DataSnapshot deeperSnapshot : dataSnapshot) {
            snapshot.getKey();
            // if snapshot.getKey() is "users", this will have value of String: "randomUserId1", then "randomUserId2"
            // If snapshot.getKey() is "books", this will have value of String: "bookId1", then "bookId2"
        }
    }
}

由 addChildEventListener 生成的 DataSnapshot

addChildEventListener() 生成的 dataSnapshot 将包含数据值,这些数据在被引用的数据中更深一层。就像在这些情况下:

ref 指向 your-project-name 然后 dataSnapshot 应该是:

... onChildAdded(DataSnapshot dataSnapshot, String s) {
    dataSnapshot.getKey(); // will have value of String: "users", then "books"
    for (DataSnapshot snapshot : dataSnapshot) {
        snapshot.getKey();
        // if dataSnapshot.getKey() is "users", this will have value of String: "randomUserId1", then "randomUserId2"
        // If dataSnapshot.getKey() is "books", this will have value of String: "bookId1", then "bookId2"
        for (DataSnapshot deeperSnapshot : dataSnapshot) {
            snapshot.getKey();
            // if snapshot.getKey() is "randomUserId1" or "randomUserId1", this will have value of String: "display-name", then "gender"
            // But the value will be different based on key
            // If snapshot.getKey() is "books", this will have value of String: "title", but the value will be different based on key
        }
    }
}
// dataSnapshot inside onChildChanged, onChildMoved, and onChildRemoved will have the same data as onChildAdded

我知道我们很可能会想要使用 .getValue() 而不是 getKey()。但是在这里我们使用 getKey 因为它总是包含一个 String 而不需要转换成自定义对象,Map 或其他。基本上,当你知道 dataSnapshot 指向哪个键​​时,你可以很容易地知道它包含哪个值并将其解析为你自己的自定义对象(或任何东西)