瞭解 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 指向哪個鍵​​時,你可以很容易地知道它包含哪個值並將其解析為你自己的自定義物件(或任何東西)