Firebase Swift childByAutoId() [英] Firebase Swift childByAutoId()

查看:95
本文介绍了Firebase Swift childByAutoId()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Firebase文档 childByAutoId 应该在被调用时生成一个唯一的ID。



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

 让设备信息= [
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(:.value,其中:{(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(:.value,其中:{(userDevices)in
if userDeviceDict = userDevices.value as? NSMutableDictionary {

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

userDeviceRef.setValue(userDeviceDict)
}
})
$ b $其他$ {
let alert = UIAlertController(title:Sorry,message:您只能添加三个设备,preferredStyle:.alert)
alert.addAction(UIAlertAction title:Ok,style:.default){_in})
self.present(alert,animated:true){}
$ b}
} else {
self.ref.child(Devices)。child(FIRAuth.auth()!. currentUser!.uid).setValue([Device1:DeviceInfo])


self.ref.child(UserDevic )。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 beed succssfully added ,preferredStyle:.alert)
alert.addAction(UIAlertAction(title:Ok,style:.default){_in})
self.present(alert,animated:true){}
}
})

//
}})
}

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

如果设备少于3个,用户可以添加( else case ),否则应该向用户 添加到Firebase。但第二次,当我试图添加第二个设备,第一个字典已被添加,但不是第二个包含 childByAutoId



这听起来很奇怪。 childByAutoId 给每个注册用户一个id? $ b

我问为什么childByAutoId()没有生成自动ID第二个
时间我添加了一个设备?

看起来你只是在Devices为空时调用 childByAutoId()(即当 snapShot.exists() code>返回false)。一旦你填充设备,该行不会再被击中。


According to the Firebase Docs, childByAutoId should generate a unique id whenever it's called.

Ok, but I'm using it here like this:

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){}
                                                       }
                    })

                    //
                } })
        }

This is a part of a button for adding a device to Firebase.

If there are less than 3 devices, the user can add (else case here), otherwise, an alert should appear to the user (you can add only 3 devices) 'if case before this else'.

When I added the first device, the two above dictionaries have been added to Firebase. But the second time, when I tried to add the second device, the first dictionary has been added, but not the second one that contains the childByAutoId.

That sounds weird for me. Can childByAutoId give one id for each register user?

解决方案

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

It looks like you're only calling childByAutoId() when "Devices" is empty (i.e. when snapShot.exists() returns false). Once you've populated "Devices", that line will not get hit again.

这篇关于Firebase Swift childByAutoId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆