0

To explain my question with a sample: I have a list view in which I need to display a person's id and name. Ids are stored locally in SQLite and I need to get the corresponding names from Firebase. What is the best approach in this case? If the Ids and Names were both from Firebase, then Firebase UI would be the answer but what would be the best way for my use case of "looking up" data, since synchronous queries are not possible with Firebase?

The way I'm doing right now is using addListenerForSingleValueEvent to populate a Hashmap with the dataSnapshot and use the Hashmap for subsequent lookups. This way I think I can avoid too many network requests. Is this ideal? I do use persistence and keepSynced(true) with this approach, but my question is specifically w.r.t. "looking up" data and avoid using an extra Hashmap.

Chinni Krishna
  • 43
  • 1
  • 2
  • 7

1 Answers1

0

If this approach solves your problem, then it's ideal for you. Because every node in a Firebase database is a Map, what you doing is actually the way it can be done. You cannot do something more to have less network connections.

But if I were you, I've added the both, Ids and Names in the Firebase database to simplify things.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • List of Ids is a subset and dynamic list (along with some other values) for each user of app. Persons Data stored on Firebase is the id-name mapping. That is why I called this a "lookup". I need to fetch only required persons dynamically. ListView gets built item by item, so querying Firebase for each id and listening to name data asynchronously doesnt seem right. – Chinni Krishna Feb 09 '18 at 10:37
  • Yes but you can query only once and add the logic inside `onDataChange()` method or if you want, you can create your own callback as in the last part of my answer of this [post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). – Alex Mamo Feb 09 '18 at 11:17