gpt4 book ai didi

swift - GeoFire 查询完成 - Swift

转载 作者:行者123 更新时间:2023-11-28 15:38:00 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 GeoFire 来查询用户周围的用户位置。这是一个类似 Tinder 的应用程序,带有卡片等...我使用 KolodaView 来制作卡片。

查询函数:

func queryUsers(searchRadius: Int, center: CLLocation) {

print("Query started")

let geofireRef = Database.database().reference().child("usersLocations")
let geoFire = GeoFire(firebaseRef: geofireRef)

let circleQuery = geoFire?.query(at: center, withRadius: Double(searchRadius))

_ = circleQuery?.observe(.keyEntered, with: { (result, location) in

print("User \(result!) found")
print("Adding user \(result!)")

addUser(userID: result!, completionHandler: { (success) in

print("User added")

})

})

}

添加用户功能:

func addUser(userID: String, completionHandler:@escaping (Bool) -> ()) {

let foundUsers = UserDefaults.standard.array(forKey: "foundUsers")

let databaseRef = Database.database().reference()

databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in

if (foundUsers?.count)! < 20 {

// USERS

let users = (snapshot.value as? NSDictionary)?["users"] as! NSDictionary

// USER

let user = users[userID] as! NSDictionary

getFirstUserPicture(userID: userID, completionHandler: { (data) in

if let data = data {

DispatchQueue.main.async {

user.setValue(data, forKey: "picture")

// APPEND FOUND USERS ARRAY

var foundUsers = UserDefaults.standard.array(forKey: "foundUsers")

foundUsers?.append(user)

// STORE FOUND USERS ARRAY

UserDefaults.standard.set(foundUsers, forKey: "foundUsers")
UserDefaults.standard.synchronize()

// UPDATE CARD VIEW

if foundUsers?.count == 1 {

NotificationCenter.default.post(name: .loadCardView, object: nil)

} else {

NotificationCenter.default.post(name: .loadCardsInArray, object: nil)

}

// COMPLETION

completionHandler(true)

}

}

})

}

}) { (error) in

print(error.localizedDescription)

}

}

当我启动应用程序时,queryUsers 函数被调用,查询开始

输出

User 1XXXXXXXXXXXXXXXX found
Adding user 1XXXXXXXXXXXXXXXX
User 2XXXXXXXXXXXXXXXX found
Adding user 2XXXXXXXXXXXXXXXX
User added
User added
  1. 找到用户
  2. 添加用户(调用addUser函数)

问题是它没有等待 addUser 完成为找到的第二个用户调用 addUser。结果是在我的KolodaView中,有第二个用户找到了两次因为我认为为找到的第二个用户调用 addUser 使用了第一个用户找到的参数。

是否可以等待第一个 addUser 完成并重新开始查询?或者只是在找到第一个用户后“暂停”查询,并在第一个 addUser 调用完成后再次启动查询?

谢谢你的帮助

更新1

我尝试了@Rlee128 解决方案但没有任何改变,我有相同的输出:( :

// MARK: UPDATE USER LOCATION - FUNCTION

func updateUserLocation() {

let databaseRef = Database.database().reference().child("usersLocations")
let geoFire = GeoFire(firebaseRef: databaseRef)

let userID = UserDefaults.standard.string(forKey: "userID")!

let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingLocation()

geoFire?.setLocation(locationManager.location, forKey: userID, withCompletionBlock: { (error) in

if error != nil {

print(error as Any)

} else {

// Location updated

}

})

}


// MARK: LOCATION MANAGER DELEGATE - FUNCTION

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

manager.stopUpdatingLocation()
manager.delegate = nil

}

最佳答案

在调用 manager.stopUpdatingLocation() 之后,location manger didUpdateLocations 将 manager.delegate 设置为 = nil。如果您想让我向您展示它在我的代码中的外观,请告诉我。

func findLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
manager.delegate = nil
let loc = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
getLocation(location: loc)

}

关于swift - GeoFire 查询完成 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44085598/

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