0

I'm working on iOS application using swift and firebase.

I'm having a view called add device, where the registered user can add a device. Simply he/she can enter the device name, description, category and add the image then click on add device button.

Here's the current dashboard that contains the info of the registered users:

{
"UserProfile" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
  "city" : "الرياض",
  "email" : "noura@gmail.com",
  "name" : "Norah",
  "phone" : "0564695353"
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
  "city" : "Riyadh",
  "email" : "mawada2017@gmail.com",
  "name" : "Mawada",
  "phone" : "0534260282"
  }
 }
}

I want to add the device info into another node.

Here's the button:

     @IBAction func AddDeviceButton(sender: AnyObject) {

    if DeviceName.text == "" || Description.text == "" || ImageView.image == nil {
        let alert = UIAlertController(title: "عذرًا", message:"يجب عليك تعبئة معلومات الجهاز كاملة", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}

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

                let profileImageUrl = metadata?.downloadURL()?.absoluteString

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

                    self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

            })






}

}

In this line:

  self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

The view could not recognize the uid!

Why?

Mariah
  • 573
  • 3
  • 10
  • 26
  • Btw mariah , the way you are trying to make Devices node , i think you will end up with only one device value replacing the previous one , that too not in the specific device i.e `Device1` node... – Dravidian Aug 27 '16 at 16:40
  • @ Dravidian I Know, I thought it's two questions in one so that's why you did not answer the second one. I'm trying to find a way to make that works. Can you help? – Mariah Aug 27 '16 at 16:50
  • @ Dravidian I'm still not sure about the structure of JSON, I feel like this is a nested structure! – Mariah Aug 27 '16 at 16:51
  • post another Q..as for nested structure, read my comment on http://stackoverflow.com/a/38959498/6297658 – Dravidian Aug 27 '16 at 16:56

1 Answers1

1

Just replace

self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

with

self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).setValue(DeviceInfo)
Dravidian
  • 9,945
  • 3
  • 34
  • 74