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"가리키는 동일한 결과를 갖습니다.FirebaseDatabase.getInstance().getReference("users/randomUserId1")및FirebaseDatabase.getInstance().getReference().child("users/randomUserId1")FirebaseDatabase.getInstance().getReference().child("users").child("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에서 데이터를 가져 오는 세 가지 일반적인 방법이 있습니다 :
-
addValueEventListener() -
addListenerForSingleValueEvent() -
addChildEventListener()
어떤 데이터가 dataSnapshot 객체 안에 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"
}
}
}
addValueEventListener 및 addListenerForSingleValueEvent에 의해 생성 된 DataSnapshot
dataSnapshot addValueEventListener() 및 addListenerForSingleValueEvent() 의해 생성 된 addValueEventListener() 은 참조되는 정확한 데이터의 값을 포함합니다. ref 가 "your-project-name" 을 dataSnapshot 때처럼 dataSnapshot 은 다음 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 때 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
우리가 getKey() 대신 .getValue() 를 사용하려고 할 가능성이 가장 높습니다. 그러나 여기서 항상 getKey 를 사용합니다. 왜냐하면 항상 하나의 문자열을 포함 할 것이고 사용자 정의 객체 또는 Map 또는 다른 것으로 변환 할 필요가 없기 때문입니다. 기본적으로, 어떤 dataSnapshot 이 가리키고있는 키인지 알게되면, 그 값이 포함하고있는 값을 쉽게 알 수 있고 자신의 커스텀 obeject (또는 아무것도)로 파싱 할 수 있습니다.