2

I'm a big fan of Firebase and that is the reason I've decided to use it in my current app, but I've got to a point where I no longer know if it can support my needs.

App functionality is the same as "Tinder" app, you can swipe like/unlike on people based on their gender, location, and age.

I've noticed there is no reason having more than one query at a time, so I'm saving each person at a path based on its gender, and the "key" is a "push key" firebase generates. I'm using the "push key", because It's indexed, so the user will not swipe twice the same user. (Swiping will start from the first user that joined the system)

let lookForGender = "female"
//swipingIndex is the last user index I've swiped.
let baseRef = firebaseApp.database().ref("lookForGender")
                         .startAt(null, currentUser.swipingIndex);

baseRef.once('value', function (snapshot) {
      UsersFeedService.updateState(snapshot);
})

//DB looks like:

"female"
       someIndexKey
            userObject
       someIndexKey
            userObject
"male"
       someIndexKey
            userObject
       someIndexKey
            userObject

This works well, but now I'm looking for a way of getting only people near me, for that I've added “GeoFire”, but It has to work on its own “key”. And I can not save users by location keys because then they will not be indexed (So user will only swipe once on each user).

Geo location would like the db to look something like this:

“location”
     userObject
“location”
     userObject
“location”
     userObject
“location”
     userObject

Getting a snapshot of all users and iterating between them is not an option since they maybe are a lot of users.

Is this something that's possible to achieve with firebase? I'm I not structuring the data in the right way? And if so, how should I?

MCMatan
  • 8,623
  • 6
  • 46
  • 85
  • 2
    Downvoter, could explain why instead of just downvoting? Thanks. – MCMatan Feb 12 '17 at 16:32
  • 3
    My guess is that the down voter thought the question was vague; what is the actual question? Geofire was designed to do what you appear to need - the problem is the Firebase structure you've implemented; there's no reason to group all females together and all males together, where's your GeoFire data? How are you associating a location with a user? All of these things need to be part of your structure and your queries will naturally filter out users that are outside the area of this user. – Jay Feb 12 '17 at 16:38
  • @Jay thank you for the response, I've edited my question to be more precise about what is the question, and it is about structure.. – MCMatan Feb 12 '17 at 17:05

1 Answers1

3

How about a typical design pattern in firebase?

users
  uid_0
   gender: "male"
   name: "George"
   age: "35"
  uid_1
   gender: "female"
   name: "Gracie"
   age: "32"

_geofire
  uid_0:
    g: "asdaseeefef"
    l: 
      0: 52.2101515118818
      1: -0.3215188181881
  uid_1:
    g: "oposooksok"
    l: 
      0: 50.1234567898788
      1: -0.8789999595988

With this structure you can query for all males, females and/or all people within a radius of the user (per the question).

With a little code you can query all females within the user's radius, people by age or anyone named Gracie.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • 1
    Hi, thanks for your response, And I've found this: https://groups.google.com/forum/#!msg/firebase-talk/AKj-bNa3igo/mA-VUIFHAAAJ that is basically said by firebase, that there is no way of querying the database without going throw all users in the current area and client filtering them.. my problem is, they maybe 5000 users it an area of a few kilometers, and I'm too much process for a client to loop every single one of them.. – MCMatan Feb 12 '17 at 17:36
  • 1
    @MCMatan There are a number of ways to do what you want. You could, for example have a geoFire node for males and one for females. You could structure you geofire node names (keys) by combining the gender, uid and age, like male:uid_0:35. See [Filtering Geofire](http://stackoverflow.com/questions/34347571/filtering-results-with-geofire-firebase?lq=1) and the answer from ZenPylon. And also [This](https://github.com/firebase/geofire-js/issues/40) and the answer from jwngr. Lastly, iterating through 5000 is very fast so that should not be a holdup. – Jay Feb 12 '17 at 18:11
  • @MCMatan Note that while what Jacob is stating is accurate - there are a number of ways to query Firebase for multiple parameters. – Jay Feb 12 '17 at 18:13
  • thank you so much for the reference, it is a great way of making it more efficient, and I'm trying to make the app ready for more then 5,000 users, maybe 10,000 in an area and more. But, the answer does not cover the option of not going throw the same people again. for that I will maybe consider adding for each user an array of "passed users ids" and filter that out too... anyway, thanks! – MCMatan Feb 12 '17 at 18:28