您好我正在进行firebase调用,这是一个查询以检查与NSDictionary值相关的相同帖子。快照打印我想要的帖子,但只显示1个帖子。我想到为什么它只显示1个帖子,但是这个时候很难。
func getAllPosts(refreshing: Bool, refreshControl: UIRefreshControl?) {
MBProgressHUD.showAdded(to: self.view, animated: true)
Database.database().reference().child("posts").child(self.otherUser?["uid"] as! String).queryOrdered(byChild: "uid").queryEqual(toValue: self.otherUser?["uid"] as! String).observe(.childAdded, with: { snapshot in
if snapshot.exists() {
print(snapshot)
if let dictionary = snapshot.value as? [String: AnyObject] {
self.feeds = []
let uid = dictionary["uid"] as? String
let name = dictionary["businessName"] as? String
let address = dictionary["businessStreet"] as? String
let state = dictionary["businessCity"] as? String
let caption = dictionary["caption"] as? String
let downloadURL = dictionary["download_url"] as? String
let timestamp = dictionary["timestamp"] as? Double
let date = Date(timeIntervalSince1970: timestamp!/1000)
let postID = dictionary["postID"] as? String
let lat = Double(dictionary["businessLatitude"] as! String)
let long = Double(dictionary["businessLongitude"] as! String)
let businessLocation = CLLocation(latitude: lat!, longitude: long!)
let latitude = self.locationManager.location?.coordinate.latitude
let longitude = self.locationManager.location?.coordinate.longitude
let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)
let distanceInMeters: Double = userLocation.distance(from: businessLocation)
let distanceInMiles: Double = distanceInMeters * 0.00062137
let distanceLabelText = String(format: "%.2f miles away", distanceInMiles)
let post = Post(uid: uid!, caption: caption!, downloadURL: downloadURL!, name: name!, date: date, address: address!, state: state!, distance: distanceLabelText, postID: postID!)
self.feeds.append(post)
// sort posts by date
self.feeds.sort {$0.date.compare($1.date) == .orderedDescending}
self.feedTableView.reloadData()
}
if refreshing {
refreshControl?.endRefreshing()
}
MBProgressHUD.hide(for: self.view, animated: true)
} else {
print("No Snapshot found")
MBProgressHUD.hide(for: self.view, animated: true)
}
})
}