Swift Firestore GeoFire - 如何在具有大型数据库的位置应用程序中正确使用以获得最佳性能?

问题描述 投票:0回答:0

您好,我正在开发一个基于位置的应用程序(即 uber、doordash、tinder、bumble 等),预计该应用程序将拥有来自世界各地的大量用户数据库。我是 Xcode + swift 和 firebase 的新手,我目前正在使用 Firestore。我最近发现了 GeoFire 并使用了哈希,但似乎我发现的大部分信息都已过时。

我想了解的是构建数据库和代码以实现最佳性能和降低成本的正确方法。

我当前的数据库看起来像这样:

FirestoreDB -> Users -> UserId -> Document Data = [ Latitude, Longitude, GeoHash ]

我使用的代码来自 Firebase 文档(*我目前没有任何关于城市、国家等的收藏/文档)-

// Compute the GeoHash for a lat/lng point
let latitude = 51.5074
let longitude = 0.12780
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

let hash = GFUtils.geoHash(forLocation: location)

// Add the hash and the lat/lng to the document. We will use the hash
// for queries and the lat/lng for distance comparisons.
let documentData: [String: Any] = [
    "geohash": hash,
    "lat": latitude,
    "lng": longitude
]

let londonRef = db.collection("cities").document("LON")
londonRef.updateData(documentData) { error in
    // ...
}

在同一个文档中,查询代码-

// Find cities within 50km of London
let center = CLLocationCoordinate2D(latitude: 51.5074, longitude: 0.1278)
let radiusInM: Double = 50 * 1000

// Each item in 'bounds' represents a startAt/endAt pair. We have to issue
// a separate query for each pair. There can be up to 9 pairs of bounds
// depending on overlap, but in most cases there are 4.
let queryBounds = GFUtils.queryBounds(forLocation: center,
                                      withRadius: radiusInM)
let queries = queryBounds.map { bound -> Query in
    return db.collection("cities")
        .order(by: "geohash")
        .start(at: [bound.startValue])
        .end(at: [bound.endValue])
}

var matchingDocs = [QueryDocumentSnapshot]()
// Collect all the query results together into a single list
func getDocumentsCompletion(snapshot: QuerySnapshot?, error: Error?) -> () {
    guard let documents = snapshot?.documents else {
        print("Unable to fetch snapshot data. \(String(describing: error))")
        return
    }

    for document in documents {
        let lat = document.data()["lat"] as? Double ?? 0
        let lng = document.data()["lng"] as? Double ?? 0
        let coordinates = CLLocation(latitude: lat, longitude: lng)
        let centerPoint = CLLocation(latitude: center.latitude, longitude: center.longitude)

        // We have to filter out a few false positives due to GeoHash accuracy, but
        // most will match
        let distance = GFUtils.distance(from: centerPoint, to: coordinates)
        if distance <= radiusInM {
            matchingDocs.append(document)
        }
    }
}

// After all callbacks have executed, matchingDocs contains the result. Note that this
// sample does not demonstrate how to wait on all callbacks to complete.
for query in queries {
    query.getDocuments(completion: getDocumentsCompletion)
}

据此,对于我的情况,我需要为每个城市/位置(?)创建一个集合并查询数据库中的所有“userId”文档,这当然不是最佳选择。我看到保存key和key检索的函数,比如-

geoFire.getLocationForKey("firebase-hq") 

但是我没有看到密钥被添加到 Firestore,我不确定这在我的情况下会如何工作,因为任何位置和用户的位置都需要动态并且需要随时更新。

所以我不确定最好的方法是什么,那里有很多流行的基于位置的应用程序,所以我想知道他们如何处理它。任何建议表示赞赏,谢谢。

swift xcode firebase google-cloud-firestore geofire
© www.soinside.com 2019 - 2024. All rights reserved.