1

I'm using the new Google Firebase database in my iOS (Swift) app.

When the user create an account, he is added to the database with some parameters like his name, his age, and mostly his current location (latitude / longitude).

I would like the app to find other users within XXkm around him.

I created the test users like this :

for index in 1...10 {

        let profile: NSMutableDictionary = [

            "username" : "User\(index)",

        ]

        let parameters = [ // IT IS PARIS LOCATION

            "latitude" : 48.856614,
            "longitude" : 2.352222

        ]

        self.ref.child("usersTest").child("userID:\(index)").child("profile").setValue(profile)
        self.ref.child("usersTest").child("userID:\(index)").child("parameters").setValue(parameters)

    }

This is how I proceed :

// MARK: GET RANDOM USER

    refHandle = ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in


        // IN THE DATABASE, I MODIFY THE USER 1 TO BE MY OWN ACCOUNT,
        // SO I CHANGE THE LOCATION TO BE DIFFERENT FROM THE OTHER
        // USERS LOCATION

        let ownUserID = "userID:1"

        let usersDict = snapshot.value!["usersTest"] as! NSDictionary

        let ownLatitude = usersDict[ownUserID]!["parameters"]!!["latitude"] as! CLLocationDegrees
        let ownLongitude = usersDict[ownUserID]!["parameters"]!!["longitude"] as! CLLocationDegrees

        let ownMaxDistance = usersDict[ownUserID]!["parameters"]!!["max_distance"] as! CLLocationDegrees

        self.myLocation = CLLocation(latitude: ownLatitude, longitude: ownLongitude)


        let usersID = usersDict.allKeys as! [String]

        for index in 0...usersID.count - 1 {

            let foundUserID = usersID[index]

            if foundUserID != ownUserID {

                let users = usersDict[foundUserID] as! NSDictionary

                self.usersArray["user\(index)"] = users

                let userParameters = self.usersArray["user\(index)"]!["parameters"] as! NSDictionary

                let latitude = userParameters["latitude"] as! CLLocationDegrees
                let longitude = userParameters["longitude"] as! CLLocationDegrees

                let userLocation = CLLocation(latitude: latitude, longitude: longitude)

                let distance = self.getDistance(userLocation, city2: self.myLocation)

                if distance < ownMaxDistance {

                    print("The user is inside your radius")

                } else {

                    print("The user is outside your radius")

                }

            }

        }

    })


// MARK: GET DISTANCE - FUNCTION

    func getDistance(city1: CLLocation, city2: CLLocation) -> CLLocationDegrees {

        let distance: CLLocationDistance = (city1.distanceFromLocation(city2)) / 1000

        return distance

    }

The problem with this way is that all the database is loaded because the app load the database, and then it check if the found user is inside or outside your radius (ownMaxDistance).

I do not know how to classify found users in ascending order based on the distance between the found user and the user.

With 100/200 users in the database there is no problem, but if the database hold thousands of users, it will be very long to load all the users!

Thanks for your help !


UPDATE 1

To be clear, it is a Tinder like app

fredericdnd
  • 968
  • 1
  • 14
  • 25

2 Answers2

1

I found the solution ! I used the old version of Firebase (2.5.1) to install GeoFire, I didn't use Cocoapods, I installed all the frameworks manually with a bridging-header

Link of the old Firebase : https://www.firebase.com/docs/ios/

Ask me if you have troubles with the installation of the frameworks ;)

fredericdnd
  • 968
  • 1
  • 14
  • 25
0

Since you are already using Firebase. I think you might be able to use this. Its called GeoFire. Its a addon to Firebase. Most likely would be the quickest/easiest way to move forward.

https://firebase.googleblog.com/2013/09/geofire-location-queries-for-fun-and.html https://github.com/firebase/geofire-objc

Also a question on here that might also help GeoFire query on User location

Community
  • 1
  • 1
the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
  • Thanks for your answer, I found GeoFire but I was very bother when I discovered that it is not ready for Firebase 3.0, so I can't use it for the moment, I want to find an another solution, but I don't find it. Perhaps I just need to wait the release of the new GeoFire version ? – fredericdnd Jun 04 '16 at 17:38
  • Don't know what to say. Depends on your deadline, also when they will update for Firebase 3.0. The other way that might work is complicated, maybe using a "ground zero" type GPS Coordinates that you calculate the distance to for each user(standard base value to work from). Then with this value you can try to filter the results from Firebase based on how close people are to this GPS point. Its hard to explain.lol. – the_pantless_coder Jun 04 '16 at 17:43
  • I have no deadline, it's a personal project, but I will inquire about this "ground zero" type GPS, or simply wait the release of the new GeoFire version, thank you ;) – fredericdnd Jun 04 '16 at 17:59
  • No problem, I thought about the "ground zero" idea, I don't think it would work. I am working a project that will need to be able to search based on closet location to user. Will be working on it this week or next week. When I figure it out I will share my solution :) Happy Coding. – the_pantless_coder Jun 05 '16 at 17:42
  • Why the down vote? I posted a possible solution based on the question asked and information provided. To whom ever down voted, If you feel like it is not a good answer why not post a comment or an alternative solution instead of just down voting without any constructive feedback. It does nothing to improve the quality of stack overflow. – the_pantless_coder Jun 05 '16 at 17:43
  • 1
    Thanks a lot for your generosity ! :) it's not me who down voted – fredericdnd Jun 05 '16 at 17:45
  • Perhaps I need to use an other database than Firebase ? I didn't find something working since the last answer. I repeat, I just want to find a way to avoid retrieving all the database to find a user. Actually, I retrieve the first user, the app check if the user is into the radius, if not, it retrieve a new, etc.., so with 100 users in the database, it's ok, but with hundreds of users, I could be very slow. there is there a way to sort the users in order of distance from a user? – fredericdnd Jun 08 '16 at 14:19