我正在使用swift和firebase处理iOS应用程序。
用户可以通过他/她的姓名,电子邮件,密码和其他一些信息从注册视图注册,这里是仪表板中的用户节点,每个用户都应该有一个称为用户设备的子节点存储ids'对于用户设备:
{
"Users": {
"1EJ8QmEQBJfBez7PMADbftCjVff1":{
"Email":"",
"Phone":"",
"Name":"",
"City":"",
"UserDevices": {
"1EJ8QmEQBJfBez7PM":"",
"1EJ8QmEQBJfBdfkgj":"",
"1EJ8QmEQBJfBdfkgj":""
}
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2": {
"Email":"",
"Phone":"",
"Name":"",
"City":"",
"UserDevices": {
"1EJ8Qgjhdghhgjhgd":"",
"sjdhfkhfhfhBdfkgj":""
}
}
}
}
还有一个名为add device的视图,注册用户可以在其中添加设备。只需他/她可以输入设备名称,描述,类别并添加图像,然后单击添加设备按钮。
每个用户只能添加五个设备。
所以有另一个节点叫做像这样的设备:
"Devices": {
"1EJ8QmEQBJfBez7PMADbftCjVff1":{
"DeviceName": "",
"Description": "",
"Category": "",
"ImageURL": ""
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2":{
"DeviceName": "",
"Description": "",
"Category": "",
"ImageURL": ""
}
}
我试图将此结构转换为代码。
这是注册按钮的一部分:
else {
FIRAuth.auth()?.signInWithEmail(self.EmailTF.text!, password: self.PasswordTF.text!, completion: { (user: FIRUser?, error: NSError?) in
if let error = error {
print(error.localizedDescription)
} else {
self.ref.child("UserProfile").child(user!.uid).setValue([
"email": self.EmailTF.text!,
"name" : self.NameTF.text!,
"phone": self.PhoneTF.text!,
"city" : self.CityTF.text!,
])
self.ref.child("UserProfile").child("UserDevices")
print("Sucess")
}
以下是我遇到问题的添加设备按钮的一部分:
else {
let imageName = NSUUID().UUIDString
let storageRef = FIRStorage.storage().reference().child("Devices_images").child("\(imageName).png")
if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) {
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error)
} else {
//Here I want to check of the number of the ids' of UserDevices if less than or equal to 5 the user can add device, otherwise an alert will appear to the user!
if self.ref.child("UserProfile").child("UserDevices").child(id) <= 5 {
let profileImageUrl = metadata?.downloadURL()?.absoluteString
let DeviceInfo = [
"ImageUrl":profileImageUrl!,
"DeviceName":self.DeviceName.text!,
"Description":self.Description.text!,
"Category":self.itemSelected
]
self.ref.child("Devices").childByAutoId().setValue(DeviceInfo)
self.ref.child("UserProfile").child("UserDevices").setValue(id)
} else {
print("You can only enter five devices maximum")
//alert message
}
}
})
}
}
*这是访问UserDevices ID的正确方法吗?
self.ref.child("UserProfile").child("UserDevices").child(id)
答案 0 :(得分:1)
尝试这样的事情: -
project_id