【问题标题】:Recover data without treating the entire database - Google Firebase / Swift在不处理整个数据库的情况下恢复数据 - Google Firebase / Swift
【发布时间】:2016-06-02 10:19:25
【问题描述】:

我在我的 iOS (Swift) 应用中使用新的 Google Firebase 数据库。

当用户创建一个帐户时,他会被添加到数据库中,其中包含一些参数,例如他的姓名、年龄,主要是他当前的位置(纬度/经度)。

我希望该应用能够找到他周围 XX 公里内的其他用户。

我这样创建了测试用户:

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)

    }

我就是这样继续的:

// 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

    }

这种方式的问题是所有数据库都被加载了,因为应用程序加载了数据库,然后它检查找到的用户是在你的半径之内还是之外(ownMaxDistance)。

我不知道如何根据找到的用户与用户之间的距离,按升序对找到的用户进行分类。

数据库中有100/200个用户是没有问题的,但是如果数据库有几千个用户,加载所有用户会很长!

感谢您的帮助!


更新 1

明确地说,它是一个类似于 Tinder 的应用程序

【问题讨论】:

    标签: database swift firebase location cllocation


    【解决方案1】:

    我找到了解决方案!我使用旧版本的 Firebase (2.5.1) 安装 GeoFire,我没有使用 Cocoapods,我使用桥接头手动安装了所有框架

    旧 Firebase 的链接:https://www.firebase.com/docs/ios/

    如果您在安装框架时遇到问题,请询问我 ;)

    【讨论】:

      【解决方案2】:

      因为您已经在使用 Firebase。我想你也许可以使用这个。它被称为GeoFire。它是 Firebase 的一个插件。很可能是前进的最快/最简单的方式。

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

      这里还有一个问题可能也有帮助 GeoFire query on User location

      【讨论】:

      • 感谢您的回答,我找到了 GeoFire,但是当我发现它还没有为 Firebase 3.0 准备好时,我很烦恼,所以我暂时不能使用它,我想找到另一个解决方案,但我没有找到。也许我只需要等待新版 GeoFire 的发布?
      • 不知道该说什么。取决于您的截止日期,以及他们何时更新 Firebase 3.0。另一种可能可行的方法很复杂,可能使用“零地”类型的 GPS 坐标,您可以计算每个用户的距离(工作的标准基值)。然后使用此值,您可以尝试根据人们与该 GPS 点的距离来过滤来自 Firebase 的结果。很难解释。大声笑。
      • 我没有截止日期,这是一个个人项目,但我会询问这个“零地”类型的GPS,或者干脆等待新的GeoFire版本发布,谢谢;)
      • 没问题,我想过“零地”的想法,我认为它行不通。我正在开展一个项目,该项目需要能够根据用户的壁橱位置进行搜索。这周或下周将继续努力。当我弄清楚时,我将分享我的解决方案:) 快乐编码。
      • 非常感谢您的慷慨! :) 不是我投了反对票
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2020-10-20
      相关资源
      最近更新 更多