0

What is the proper way to query for every Comment (POJO) attached to a Photo (POJO) in Android. In Firebase, I have the following structure for a photo collection:

"photos" : {
    "-KaeuYf9jXcy4oqpRqbi" : {f
      "entities" : {
        "type" : {
          "image" : {
            "bucket" : "bucket",
            "prefix" : "https://s3-us-west-2.amazonaws.com/",
            "suffix" : "image.jpg"
          }
        }
      },
      "stats" : {
        "liked" : 0
      },
      "text" : "Person Detected!",
      "timestamp" : 1484631194257
    }
}

and for comments collection:

"comments" : {
    "-KdE6Hwua8d6sBQimPos" : {
      "attachphoto" : {
        "-KaeuYf9jXcy4oqpRqbi" : true
      },
      "attachuser" : {
        "-KaeuYHjkdf9okflslf" : true
      },
      "displayName" : "Gary",
      "text" : "ok",
      "timestamp" : 1487385995844
    },
    "-KdE6IPc-NL-6zGkwXq3" : {
     "attachphoto" : {
        "-KaeuYf9jXcy4oqpRqbi" : true
      },
      "attachuser" : {
        "-KaeuYHjkdf9okflslf" : true
      },
      "displayName" : "Thomas",
      "text" : "ok",
      "timestamp" : 1487385997735
    }
}

In Android I'm using a FirebaseAdapter, but I am having trouble defining a proper Query that would only retrieve comments attached to a specific photo. I have the key of the Photo

FirebaseListAdapter adapter = new CommentAdapter(this, Comment.class, R.layout.item_comment, QUERY){
            @Override
            protected void populateView(View v, Comment model, int position) {
                if(model.getDisplayName()!=null) {
                    String[] name = model.getDisplayName().split(" ");
                    ((TextView) v.findViewById(R.id.id)).setText(name[0]);
                }
                ((TextView) v.findViewById(R.id.content)).setText(model.getText());
            }
        };

I thought I could define something like:

final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference commentsRef = database.getReference("comments");
Query query = commentsRef.orderByChild("attachphoto").equalTo()

But I'm having some disconnect on how to finish this. First project using Firebase, so any help/tips would be much appreciated!

gareoke
  • 716
  • 6
  • 17
  • 1
    It seems like you're trying to put the comments into categories. See http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Feb 18 '17 at 04:44
  • Thanks that worked! StringBuilder child = new StringBuilder("attachphoto/"); child.append(imageKey); Query query = commentsRef.orderByChild(child.toString()).equalTo(Boolean.TRUE); – gareoke Feb 18 '17 at 07:41

1 Answers1

0

Firebase is rather noSQL, therefore there are limits which one has to work around... especially when designing the data-structure. for example, in this case it might make sense to add the ids of the related users & comments (or even the related comments altogether) into a nested node of the structure, which represents the photo. there are other ways to get there - while one always needs to consider how to look up the details later on (because the structure dictates which queries are possible and which not).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216