gpt4 book ai didi

ios - 在 Swift 中使用 GeoFire 查询不会提供有用的输出

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:23 25 4
gpt4 key购买 nike

我现在非常绝望,因为我正尝试在我的 Firebase 数据库上使用 GeoFire 来查找附近的用户。我现在已经被困了两天了。我在 Google 和 stackoverflow 上搜索了很多次,并尝试了在那里找到的所有内容,但都没有成功。现在我最后的希望是自己创建这个线程,希望有人能帮助我。

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

users
user_id1
email: xxx@gmail.com
username: xxx
user_id2
email: yyy@gmail.com
username: yyy
users_locations
user_id1
location
g: xxxx
l
0: 30.0000
1: 5.0000
user_id2
location
g: yyyy
l
0: 30.0010
1: 5.0010

我不知道是否有必要将位置数据与用户分开保存,但我也尝试过这样保存它并得到相同的结果,所以我认为这无关紧要:

users
user_id1
email: xxx@gmail.com
username: xxx
location
g: xxx
l
0: 30.0000
1: 5.0000

现在我正在使用的代码:

为了将位置写入数据库,我使用了以下方法,效果很好:

let user = FIRAuth.auth()?.currentUser
let uid = user?.uid

let ref = FIRDatabase.database().reference()
let geofireRef = ref.child("users_locations")
let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!))

geoFire?.setLocation(myLocation, forKey: "location")

现在,我尝试使用 GeoFire 查询向我显示定义半径内的所有用户,但此半径不打印任何内容:

let ref = FIRDatabase.database().reference()
let geofireRef = ref.child("users_location")
let geoFire = GeoFire(firebaseRef: geofireRef)

let circleQuery = geoFire?.query(at: center, withRadius: 10)
circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
print("Key '\(key!)' entered the search are and is at location '\(location!)'")
})

我发现如果我去找我真正的 uid child ,我会得到一个结果。然后它会打印我的实际用户位置和 key ,但这当然不是我想要的。

let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!))

所以我希望 GeoFire 在“users_locations”中搜索我的 uid,并返回我定义半径内的所有 uid。

对我来说,它似乎只是在我的引用 (users_location -> user_uid) 中定义的子项中搜索名为“location”的子项,如果我尝试查询“users_location”,我什么也得不到。

我做错了什么?我怎样才能使查询搜索引用子对象并将 uid 还给我?

最佳答案

使用 Geofire 时,您有两个数据列表:

  1. 对象列表,在你的例子中是用户
  2. 这些对象的位置列表,通过 Geofire 进行维护和查询

这两个列表确实是分开的。来自Geofire documentation (强调我的):

Assume you are building an app to rate bars and you store all information for a bar, e.g. name, business hours and price range, at /bars/<bar-id>. Later, you want to add the possibility for users to search for bars in their vicinity. This is where GeoFire comes in.

You can store the location for each bar using GeoFire, using the bar IDs as GeoFire keys. GeoFire then allows you to easily query which bar IDs (the keys) are nearby. To display any additional information about the bars, you can load the information for each bar returned by the query at /bars/<bar-id>.

我针对您的问题突出显示了两个最重要的部分:

  1. 用户列表中的项目和他们的位置列表应该使用相同的键(这是安德鲁在他的回答中也指出的)。通过使用相同的 key ,您可以轻松地查找用户的位置,反之亦然。
  2. 您需要为查询中的每个键单独加载用户。

Andrew 展示了如何为每个用户正确编写位置。剩下的就是加载关于结果范围内每个用户的附加信息。你会在他.keyEntered处理程序:

let usersRef = ref.child("users")

let circleQuery = geoFire?.query(at: center, withRadius: 10)
circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in

print("Key '\(key!)' entered the search are and is at location '\(location!)'")
// Load the user for the key
let userRef = usersRef.child(key)
userRef.observeSingleEvent(FIRDataEventType.value, with: { (snapshot) in
let userDict = snapshot.value as? [String : AnyObject] ?? [:]
// ...
})
})

关于ios - 在 Swift 中使用 GeoFire 查询不会提供有用的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42810068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com