我创建了一个基本的Google地方信息应用,可让用户签到一个位置。当用户尝试签入时,我会遍历所有可能的地点列表,以验证用户是否确实在应用程序中的位置。但是,当我在确认位置正确后尝试退出循环时,我的功能仍然会转到“其他”情况(错误消息,要求用户请签入正确的位置)。
在viewWillAppear中调用以下函数:
func checkIn(handleComplete:@escaping (()->())){
guard let currentUserID = User.current?.key else {return}
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.rawValue))!
placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: fields, callback: {
(placeLikelihoodList: Array<GMSPlaceLikelihood>?, error: Error?) in
if let error = error {
print("An error occurred: \(error.localizedDescription)")
return
}
if let placeLikelihoodList = placeLikelihoodList {
for likelihood in placeLikelihoodList {
let place = likelihood.place
if likelihood.likelihood >= 0.75 && place.placeID! == self.hangoutID {
let place = likelihood.place
print("Current Place name \(String(describing: place.name!)) at likelihood \(likelihood.likelihood)")
print("Current PlaceID \(String(describing: place.placeID!))")
self.delta = 0.0
// update checkin
DispatchQueue.main.async {
let hangoutRef = self.db.collection("users").document(currentUserID).collection("hangout").document(self.hangoutID).updateData([
"lastCheckin": Date()
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
handleComplete()
}
}
self.presentDismissableAlert(title: "", message: "Please check in to the hangout to join this chat", button: "OK", dismissed: { (UIAlertAction) in
self.performSegue(withIdentifier: "unwindSegueToChats", sender: self)
})
}
})
}
如果满足正确的条件,代码将落在handleComplete()行上,但仍将在下面执行dismissableAlert并将用户隔离在房间外。如何解决流问题,以便应用程序将在可能的位置列表中循环显示,并在满足正确条件的情况下在handleComplete上停止该功能,否则,如果未满足正确条件(而不是用户),则继续执行错误消息在正确的位置)?
谢谢