我正在尝试从firebase中提取所有用户帖子,根据相对于当前用户位置的帖子位置填充TableView。
我写了一个方法来确定用户与存储在firebase中的帖子位置之间的距离。
*我重写了封装所有填充tableview数据的方法;但它仍未找到“userLocation”*
的值 func populateTableView() {
let userId = Auth.auth().currentUser?.uid
Database.database().reference().child("posts").child(userId!).observeSingleEvent(of: .value, with: { (snapshot) in
let data = snapshot.value as? [String: AnyObject]
let postLocation = data!["userLocation"] as? CLLocation //this is where it throws an error(found nil while unwrapping)
let distanceFromPost: CLLocationDistance = self.locationManager.location!.distance(from: postLocation!)
if self.metersInTwentyFiveMiles > distanceFromPost {
self.inRangeOf = true
Database.database().reference().child("posts").observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
self.posts.removeAll()
for data in snapshot {
print(data)
if let postDict = data.value as? [String: AnyObject] {
let key = data.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
}
self.tableView.reloadData()
}
}, withCancel: nil)
} else {
self.inRangeOf = false
print("no posts nearby")
}
})
}
现在在我的viewDidLoad中我刚刚调用了populateTableView函数
self.populateTableView()
我有很多帖子存储在firebase中,位于模拟器的确切位置,它只是没有在功能中拾取它们。
firebase中的数据结构如下:
-posts
-L922cw8LC2fpxLI1zZH
imageUrl:
likes:
userImg:
userLocation:
username:
任何人都可以帮助我吗? populateTableView方法是否可以从正确的子节点读取?
答案 0 :(得分:1)
我不知道我是否理解你打算做什么,但据我所知:
if self.metersInTwentyFiveMiles < distanceFromPost {
这应该是
if self.metersInTwentyFiveMiles > distanceFromPost {
要达到范围:Máximum距离&gt;当前距离