0

While going down in the firebase's structure data page, there is an example of users<->groups relationship. It is a nice way to store data that contains two-way-relationship.

Here is the relation explained on that page:

// An index to track Ada's memberships
{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
      "name": "Historical Tech Pioneers",
      "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

By storing data that way, how can I query that data to show the list of groups of a particular user?

How can I query that data? equalTo() can not do that as it takes only one key.

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • You should iterate over every key in `userID.groups` and then retrieve each group's data https://firebase.google.com/docs/database/android/retrieve-data#sort_data – Devid Farinelli Jul 01 '16 at 09:58
  • Is there any other way? So that I can bind that to the recycler view of `firebase-ui`? – kirtan403 Jul 01 '16 at 10:24
  • Sorry, but I've never used `firebase-ui` so I couldn't help on this – Devid Farinelli Jul 01 '16 at 10:27
  • 1
    You'd load each the user's group index and then load each group in turn. Performance of those inner listens won't be a problem, because the [requests are pipelined](http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786). For an example how to do this with FirebaseUI for Android, see my [answer here](http://stackoverflow.com/questions/34559171/coupling-firebaserecyclerviewadapter-to-a-boolean-string-map-entry). – Frank van Puffelen Jul 01 '16 at 14:29
  • @FrankvanPuffelen Thanks! That was exactly what I was looking for! Is there any performance improvement because, it will bind event listener everytime it comes on the screen? – kirtan403 Jul 02 '16 at 13:58
  • Setting `setPersistenceEnabled` to true will have any performance improvement? – kirtan403 Jul 02 '16 at 13:59
  • @FrankvanPuffelen With a large list there are some problem with the live updates. It changes the value to another object when I changes for some other object, as it uses a recycled view which was used for some other object before! Any solution? `onViewRecycled` should help, but how? – kirtan403 Jul 02 '16 at 14:04

0 Answers0