繁体   English   中英

Firebase Swift childByAutoId()

[英]Firebase Swift childByAutoId()

根据Firebase DocschildByAutoId每当被调用时都应该生成一个唯一的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个,则用户可以添加( 此处为case ),否则,应向用户显示警报( 您只能添加3个设备 )“如果在此之前为case”。

当我添加第一台设备时,以上两个字典已添加到Firebase。 但是第二次,当我尝试添加第二个设备时,已经添加了第一个字典,但是没有添加第二个包含childByAutoId

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

我在问为什么childByAutoId()在第二次添加设备时没有生成自动ID?

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

在添加第一个设备时,您将陷入snapShot.exists() else条件,但是在添加第二个和第三个设备时,您的snapShot.exists()条件将为true 因此,如果条件块也需要添加第二和第三UserDevices条目。 因此,您只需要更新以下几行代码:

if numberOfDevicesAlreadyInTheDB < 3 {
     let newDevice = String("Device\(numberOfDevicesAlreadyInTheDB+1)")
     let userDeviceRef = self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid) 
     userDeviceRef.updateChildValues([newDevice: DeviceInfo])
     // Also you need to log your second/third device entry here
     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])
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM