gpt4 book ai didi

swift - 从 Firebase [GeoFire] 中删除位置 - Swift

转载 作者:行者123 更新时间:2023-11-28 07:46:18 26 4
gpt4 key购买 nike

我有两个屏幕,比如 A 和 B。在屏幕 A 上,我将用户的当前位置存储在我的 Firebase 数据库中。

代码

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

SVProgressHUD.show(withStatus: "Fetching your location")

backButton.isEnabled = false
competitiveMatch.isEnabled = false
friendlyMatch.isEnabled = false
tournamentButton.isEnabled = false
trainingButton.isEnabled = false
nextButton.isEnabled = false

let location = locations[locations.count - 1]

if location.horizontalAccuracy > 0 {
self.locationManager.stopUpdatingLocation()

let userLatitude = location.coordinate.latitude
let userLongitude = location.coordinate.longitude


saveCurrentLocation(long: userLongitude, lat: userLatitude)

}
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}

函数 saveCurrentLocation 也在同一文件中声明为:

func saveCurrentLocation(long: Double, lat: Double){

let geofireRef = FIRDatabase.database().reference().child("Locations")
let geoFire = GeoFire(firebaseRef: geofireRef)

let userID = FIRAuth.auth()?.currentUser?.uid
geoFire.setLocation(CLLocation(latitude: lat, longitude: long), forKey: userID!) { (error) in
if (error != nil) {
print("An error occured: \(String(describing: error))")

self.backButton.isEnabled = true
self.competitiveMatch.isEnabled = true
self.friendlyMatch.isEnabled = true
self.tournamentButton.isEnabled = true
self.trainingButton.isEnabled = true
self.nextButton.isEnabled = true

} else {
print("Saved location successfully!")

self.backButton.isEnabled = true
self.competitiveMatch.isEnabled = true
self.friendlyMatch.isEnabled = true
self.tournamentButton.isEnabled = true
self.trainingButton.isEnabled = true
self.nextButton.isEnabled = true

SVProgressHUD.dismiss()
}
}
}

在屏幕 B 上,我有一个注销按钮。我想从 Firebase 数据库中注销用户,并在他按下按钮时也删除他保存的位置。

为此,我首先在 ViewController 类之上声明以下内容:

let userID = FIRAuth.auth()?.currentUser?.uid

然后,我使用以下代码:

func deleteUserLocation(){

let geofireRef = FIRDatabase.database().reference().child("Locations")
let geoFire = GeoFire(firebaseRef: geofireRef)

geoFire.removeKey(userID!)

print("Deleted location.")
}

//Logout Button is pressed and it takes us back to the landing page.
@IBAction func logoutPressed(_ sender: Any) {
deleteUserLocation()

do {
try FIRAuth.auth()?.signOut()
}
catch {
print("Error: There was a problem signing out!")
}
}

执行此操作后,“已删除位置”会打印在控制台中,但该位置不会从数据库中删除。

作为引用,这里是数据库的快照;

Database

最佳答案

你只需要删除数据库中的de child。

    if geoFire!= nil {
geoFire?.removeKey(userID!)
geofireRef.child(userID!).removeValue()
geoFire= nil
}

     geoFire?.firebaseRef.removeValue()

关于swift - 从 Firebase [GeoFire] 中删除位置 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50942327/

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