Firebase join data in Android

2020-04-20 08:20发布

I'm try to join three data using Firebase. I want to retrieve the the school name querying to classes data knowing only guard key which is g1. I know it can be done in two separate calls, but I'm trying to do it in just one call to the server.

Is it somewhat possible?

Schools

schools : 
  schoolKey1 :
    name: School One
  schoolKey2 :
    name: School Two

Classes

classes : 
  someKey1 :
    name: Class One
    school : schoolKey1
    guard : g1

Guard

guards : 
  g1 :
    name: Pipoy Pat

1条回答
够拽才男人
2楼-- · 2020-04-20 08:45

It might be too late but you would need to follow the documentation here.

something like:

var ref = new Firebase("https://myApp.firebaseio.com/");

    // fetch classes
    ref.child("classes").addChildEventListener(new ChildEventListener() {
      @Override
      public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        //get school key 
        String schoolKey = snapshot.getKey();

        //get school name from key 
        ref.child("schools/" + schoolKey + "/name").addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot snapshot) {
            System.out.println("The school name is " + snapshot.getValue());
          }

          @Override
          public void onCancelled(FirebaseError firebaseError) {
            // ignore for now 
          }
        });
      }
    });
查看更多
登录 后发表回答