firebase-database
データの読み込み
サーチ…
getReference()で参照されるデータの理解
この例では、このデータベースを使用します。
"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"
}
}
}
上記のデータベースを使用する場合は、次のようにします。
FirebaseDatabase.getInstance().getReference()あなたのプロジェクトの親である
"your-project-name"データを指します。したがって、取得したdataSnapshotには、すべての"users"データと"books"データを含むすべてのデータが含まれます。FirebaseDatabase.getInstance().getReference("users")およびFirebaseDatabase.getInstance().getReference().child("users")"your-project-name/users"指す同じ結果を持つでしょう"your-project-name/users"FirebaseDatabase.getInstance().getReference("users/randomUserId1")およびFirebaseDatabase.getInstance().getReference().child("users/randomUserId1")およびFirebaseDatabase.getInstance().getReference().child("users").child("randomUserId1")"your-project-name/users/randomUserId1"指して同じ結果を持ってい"your-project-name/users/randomUserId1"
注:この例は、 どのデータがdataSnapshotオブジェクト内にあるかを完全に理解するために必要です
どのデータがdataSnapshotオブジェクト内にあるかを理解する
注:この例を完全に理解するには、まずgetReference()で参照されるデータを知る必要があります 。
Firebase Realtime Databaseからデータを取得する一般的な方法は3つあります。
-
addValueEventListener() -
addListenerForSingleValueEvent() -
addChildEventListener()
どのデータがdataSnapshotオブジェクト内にあるdataSnapshotについて話すと、 addValueEventListener()とaddListenerForSingleValueEvent()は基本的に同じです。唯一の違いはaddValueEventListener()は、 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"
}
}
}
addValueEventListenerおよびaddListenerForSingleValueEventによって生成されたDataSnapshot
dataSnapshot addValueEventListener()およびaddListenerForSingleValueEvent()によって生成されるaddValueEventListener()は、それが参照される正確なデータの値が格納されます。 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には、参照先のデータのdataSnapshot深いデータの値が格納されます。これらの場合のように:
refが"your-project-name"をdataSnapshot場合、 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を使用しgetKey 。これは、常に1つのStringを含み、カスタムオブジェクト、Map、またはその他に変換する必要がないためです。基本的に、どの主要なdataSnapshotが指し示しているかを知っている場合、その値に含まれている値を簡単に知ることができ、独自のカスタムobeject(または何か)