Reputation: 95
I want to get the parent key value where the the "drinkManufacturerID" is equal to the currentUsers UID
. However, I will not know the value of the VenueID
or the PushID
(see below). How can I check if the value for "drinkManufacturerID" is equal, without knowing these values. Is it possible without restructuring my database?
Relevant Code:
public class DealRawDataActivity extends AppCompatActivity {
DatabaseReference databaseDrinks;
DatabaseReference databaseManufacturer;
String keys;
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deal_raw_data);
databaseDrinks = FirebaseDatabase.getInstance().getReference("drinks").child("-LWLuM2nesg0uaP0dLSn"); //However, this string will not be know! How to get this string value?
//To later be used in listView
databaseDrinks.orderByChild("drinkManufacturerID").equalTo(currentFirebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
keys = datas.getKey();
}
Toast.makeText(getApplicationContext(), keys, Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//databaseManufacturer = FirebaseDatabase.getInstance().getReference("drinks").child(keys);
}
}
Database Structure:
Upvotes: 3
Views: 3880
Reputation: 141
Your code is ok but it is for all the childs of a uid or anything. but when you need to get child of child of a uid.
i mean, if UID has 3 Child e.g UID-->(Child1), (child2), (Child3)
and every child has also child e.g
UID-->(Child1)-->(childA),(childB)
......(child2)-->(childC),(childD)
......(child3)-->(childD),(childE)
These child of child ( childA, childB, childC, childD, childE ) holded data and you need to get it without knowing parent key and child key, then it is very easy bro. you just need to do it
for (DataSnapshot postSnapshot1 : dataSnapshot.getChildren()){for (DataSnapshot postSnapshot2 : postSnapshot1.getChildren()){//postSnapshot2 is accessed your child of child// using recycler view, adapter class and model class you can display the whole data for all child of child}}
just take it on your code
databaseDrinks.orderByChild("drinkManufacturerID").equalTo(currentFirebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas1 : dataSnapshot.getChildren()) { for(DataSnapshot datas2 : datas1.getChildren()) { String keys = datas2.getKey();
}}
Toast.makeText(getApplicationContext(), keys, Toast.LENGTH_LONG).show();
}
@Override public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 0
Reputation: 138824
I will not know the value of the VenueID or the PushID (see below)
You need to know the value of VenueID
in order to be able to query your database at least for your the -LWLuM2nesg0uaP0dLSn
node. So the following query:
.orderByChild("drinkManufacturerID").equalTo(currentFirebaseUser.getUid())
Will only get you all the drinks that exist within that particular object.
Is it possible without restructuring my database?
No, unfortunately there is no way in Firebase to query two or more Firebase nodes, two levels deep in the tree, as you intend to. To sovle this, you should duplicate your data. This practice is called denormalization
and is a common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding.
Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
That being said, in your particular case, you should consider augmenting your data structure to allow a reverse lookup by creating another node named vanues
, where you should add as objects all corresponding drink
objects. So you database structure should look similar to this:
Firebase-root
|
--- vanues
|
--- -LZB-86uakRrWpI6VYzd (drink object)
|
--- drinkManufacturerID: "D1eY ... wrT5"
|
--- VenueID: "-LWLuM2nesg0uaP0dLSn"
Using this schema, you can simply query the database to get all drinks, where drinkManufacturerID
equal to uid
, using the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference vanuesRef = rootRef.child("vanues");
Query query = vanuesRef.orderByChild("drinkManufacturerID").equalTo(uid);
query.addListenerForSingleValueEvent(/* ... */);
If you want to get all drinks from a specific vanue, simply use:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference vanuesRef = rootRef.child("vanues");
Query query = vanuesRef.orderByChild("VenueID").equalTo(VenueID);
query.addListenerForSingleValueEvent(/* ... */);
In which VenueID
might hold a value like -LWLuM2nesg0uaP0dLSn
.
Upvotes: 6