我现在非常绝望,因为我正在尝试在 Firebase 数据库上使用 GeoFire 来查找附近的用户。
我现在已经被困了两天了。
我在 Google 和 stackoverflow 上进行了很多搜索,并尝试了在那里找到的所有内容,但没有成功。现在我最后的希望是自己创建这个线程并希望有人可以帮助我。
我的数据库几乎是这样的:
users
user_id1
email: [email protected]
username: xxx
user_id2
email: [email protected]
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: [email protected]
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 孩子,我就会得到结果。然后它打印我的实际用户位置和密钥,但这当然不是我想要的。
let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!))
因此,我希望 GeoFire 在“users_locations”中搜索我的 uid,并返回我定义的半径内的所有 uid。
对我来说,它似乎只是在我的参考(users_location -> user_uid)中定义的子项中搜索名为“location”的子项,如果我尝试查询“users_location”,我什么也得不到。
我究竟做错了什么?
如何使查询搜索引用子项并返回 uid?