我有一个聊天应用程序,并使用以下机制在“附近”检索我的用户:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
geoFireRef = Database.database().reference().child("Geolocs")
geoFire = GeoFire(firebaseRef: geoFireRef)
let userLat = UserDefaults.standard.value(forKey: "current_latitude") as! String
let userLong = UserDefaults.standard.value(forKey: "current_longitude") as! String
let location:CLLocation = CLLocation(latitude: CLLocationDegrees(Double(userLat)!), longitude: CLLocationDegrees(Double(userLong)!))
myQuery = geoFire?.query(at: location, withRadius: 100)
myQuery?.observe(.keyEntered, with: { (key, location) in
// print("KEY:\(String(describing: key)) and location:\(String(describing: location))")
SwiftOverlays.showTextOverlay(self.view, text: "Searching for nearby users...")
if key != Auth.auth().currentUser?.uid
{
let ref = Database.database().reference().child("Users").child(key!)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let id = snapshot.key
let data = snapshot.value as! [String: Any]
let credentials = data["user_details"] as! [String: String]
let name = credentials["name"]!
let email = credentials["email"]!
let latitude = credentials["current_latitude"]
let longitude = credentials["current_longitude"]
let link = URL.init(string: credentials["profilepic_url"]!)
URLSession.shared.dataTask(with: link!, completionHandler: { (data, response, error) in
if error == nil {
let profilePic = UIImage.init(data: data!)
let user = User.init(name: name, email: email, id: id, profilePic: profilePic!, latitude: latitude! , longitude:longitude! )
DispatchQueue.main.async {
SwiftOverlays.removeAllBlockingOverlays()
self.items.append(user)
self.tblUserList.reloadData()
}
}
}).resume()
})
}
else
{
DispatchQueue.main.async {
SwiftOverlays.removeAllBlockingOverlays()
}
}
})
}
不幸的是,Geofire一次返回一个键,而不是数组中的所有键。
问题在于,在收到用户数据后,此代码会执行reloadData()
,这会重装UITableView
,因为我的搜索范围可能有成千上万的用户。
我想检索数据,使用它们(应用过滤器等),完成后,做一个干净的reloadData()
。
有更好的方法吗?
(来源:https://medium.com/@hiren.patel93/using-geofire-with-firebase-in-ios-mobile-application-ce54a3fbea83)