gpt4 book ai didi

ios - Firebase Swift childByAutoId()

转载 作者:行者123 更新时间:2023-11-30 13:02:52 26 4
gpt4 key购买 nike

根据Firebase Docs , childByAutoId 每当被调用时都应该生成一个唯一的 id。

好的,但我在这里使用它是这样的:

let DeviceInfo = [
"ImageUrl":profileImageUrl!,
"DeviceName":self.DeviceName.text!,
"Description":self.Description.text!,
"Category":self.itemSelected
] as [String : Any]

self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: {(snapShot) in
if snapShot.exists(){
let numberOfDevicesAlreadyInTheDB = snapShot.childrenCount
if numberOfDevicesAlreadyInTheDB < 3{
let newDevice = String("Device\(numberOfDevicesAlreadyInTheDB+1)")
let userDeviceRef = self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid)
userDeviceRef.observeSingleEvent(of: .value, with: {(userDevices) in
if let userDeviceDict = userDevices.value as? NSMutableDictionary{

userDeviceDict.setObject(DeviceInfo,forKey: newDevice as! NSCopying)

userDeviceRef.setValue(userDeviceDict)
}
})
}
else{
let alert = UIAlertController(title: "Sorry", message:"You can only add three devices", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
self.present(alert, animated: true){}

}
}else{
self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Device1" : DeviceInfo])


self.ref.child("UserDevices").childByAutoId().setValue([
"ImageUrl":profileImageUrl!,
"DeviceName":self.DeviceName.text!,
"Description":self.Description.text!,
"Category":self.itemSelected,
"name": self.globalUserName,
"email":self.globalEmail ,
"city": self.globalCity,
"phone": self.globalPhone
] as [String : Any])

let alert = UIAlertController(title: "Great", message:"Device has beed added succssfully", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
self.present(alert, animated: true){}
}
})

//
} })
}

这是用于将设备添加到 Firebase 的按钮的一部分。

如果设备少于 3 个,用户可以添加(此处为其他情况),否则,应向用户显示警告(您只能添加 3 个设备>) 'if case 在此 else 之前'。

当我添加第一个设备时,上述两个词典已添加到 Firebase 中。但第二次,当我尝试添加第二个设备时,第一个字典已添加,但未添加包含 childByAutoId 的第二个字典。

这对我来说听起来很奇怪。 childByAutoId 可以为每个注册用户提供一个 id 吗?

最佳答案

I'm asking why childByAutoId() did not generate an auto id the second time I added a device?

看来您仅在“Devices”为空时(即当 snapShot.exists() 返回 false 时)才调用 childByAutoId()。填充“设备”后,该行将不会再次出现。

关于ios - Firebase Swift childByAutoId(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39729568/

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