sha*_*ali 0 java android json firebase firebase-realtime-database
我想只展示位置的孩子,即信德和旁遮普(不是他们的孩子).是否可能,如果可以,我该怎么做?
避免嵌套数据
由于Firebase实时数据库允许最多嵌套数据深度为32级,因此您可能会认为这应该是默认结构.但是,当您在数据库中的某个位置获取数据时,还会检索其所有子节点.此外,当您在数据库中的某个节点上授予某人读取或写入权限时,您还可以授予他们访问该节点下所有数据的权限.因此,在实践中,最好保持数据结构尽可能平坦.
以下是您的如何实现平面数据库以及如何检索位置键的结构.
{
"location": {
"punjab": {
"lahore": true,
"multan": true
},
"sindh": {
"karachi": true
}
},
"place": {
"lahore": {
"location": "punjab",
"details": {
"0": "model",
"1": "steel town"
}
},
"multan": {
"location": "punjab",
"contacts": {
"0": "myarea"
}
},
"karachi": {
"location": "sindh",
"contacts": {
"0": "hadeed",
"1": "Steel town"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是可用于检索位置键的代码.
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
mLocation = mDatabase.child("location");
mPlace = mDatabase.child("place");
ValueEventListener placeListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Place place = dataSnapshot.getValue(Place.class);
String location = place.location;
System.out.println(location);
}
};
mPlace.addValueEventListener(placeListener);
Run Code Online (Sandbox Code Playgroud)
有关Firebase的更多信息:
| 归档时间: |
|
| 查看次数: |
11163 次 |
| 最近记录: |