Ricerca…


Comprensione dei dati a cui fa riferimento getReference ()

In questo esempio, usiamo questo database:

"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"
        }
    }
}

Se si utilizza il database di cui sopra allora:

  • FirebaseDatabase.getInstance().getReference()

    indicherà i dati relativi al genitore, "your-project-name" . Quindi il dataSnapshot hai acquisito conterrà tutti i dati al suo interno, inclusi tutti i dati relativi "users" e ai "books" .

  • FirebaseDatabase.getInstance().getReference("users") e FirebaseDatabase.getInstance().getReference().child("users")

    avrà lo stesso risultato, indicando "your-project-name/users"

  • FirebaseDatabase.getInstance().getReference("users/randomUserId1") e FirebaseDatabase.getInstance().getReference().child("users/randomUserId1") e FirebaseDatabase.getInstance().getReference().child("users").child("randomUserId1")

    avrà lo stesso risultato, indicando "your-project-name/users/randomUserId1"

Nota: questo esempio è necessario per comprendere appieno quali dati si trovano all'interno dell'oggetto dataSnapshot

Comprensione dei dati all'interno dell'oggetto dataSnapshot

Nota: è necessario conoscere i dati a cui fa riferimento getReference () prima di poter comprendere completamente questo esempio.

Esistono tre metodi comuni per ottenere i dati dal database in tempo reale di Firebase:

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

Quando parliamo di quali dati si dataSnapshot nell'oggetto dataSnapshot , quindi addValueEventListener() e addListenerForSingleValueEvent() è praticamente lo stesso. L'unica differenza è addValueEventListener() continua ad ascoltare le modifiche apportate nei dati di riferimento mentre addListenerForSingleValueEvent() non lo è.


Quindi considera di avere questo database:

"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 prodotto da addValueEventListener e addListenerForSingleValueEvent

dataSnapshot prodotto da addValueEventListener() e addListenerForSingleValueEvent() conterrà valore (s) dei dati esatti a cui è fatto riferimento. Come quando ref punta a "your-project-name" quindi dataSnapshot dovrebbe essere:

... 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"
        }
    }
}

DataSnapshot prodotto da addChildEventListener

dataSnapshot prodotto da addChildEventListener() conterrà valore (s) di dati un livello più profondo all'interno dei dati a cui è fatto riferimento. Come in questi casi:

Quando ref punta a "your-project-name" quindi dataSnapshot dovrebbe essere:

... 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

So molto probabilmente che vorremmo usare .getValue() invece di getKey() . Ma qui usiamo getKey perché conterrà sempre una stringa e non è necessario convertirla in oggetto personalizzato, o mappa o altro. Fondamentalmente, quando si conosce a quale chiave dataSnapshot sta puntando, si può facilmente sapere quale valore contiene e analizzarlo nel proprio obeject personalizzato (o altro)



Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow