0

What I'm doing is exporting phone number & owner's name from iPhone address book.

Here is the code:

dbRef = Database.database().reference()


let nameData  = ["contactName": contact.name ]
let phoneData = ["contactPhone": contact.phone]


self.dbRef.child("user/contacts").childByAutoId().setValue(nameData)

self.dbRef.child("user/contacts").observe(.value, with: { (snapshot) in

     if let result = snapshot.children.allObjects as? [DataSnapshot] {

            for child in result {

                let orderID = child.key
                self.dbRef.child("user/contacts").child(orderID).updateChildValues(phoneData)
            }
        }
     })

But the result is looking like that: ContactPhone's values are changing automatically, non-stop until I stop the xCode simulator.

enter image description here

Can Somebody help me, please.. Thank you

1 Answer 1

2

What you need to do is combine the nameData and phoneData in single dictionary and set this in contact.

dbRef = Database.database().reference()
let data  = ["contactName": contact.name, "contactPhone": contact.phone ]
self.dbRef.child("user/contacts").childByAutoId().setValue(data)

Side note: Remove this observing code and put at some other place like in viewDidLoad.

Edit: If you want to set both separately then you need to get reference of that childByAutoId.

dbRef = Database.database().reference().child("user/contacts").childByAutoId()
let nameData  = ["contactName": contact.name ]
let phoneData = ["contactPhone": contact.phone]
self.dbRef.setValue(nameData)
self.dbRef.updateChildValues(phoneData)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! But Isn't it possible if i use nameData and phoneData separatly, not in single dictionary?
@KUNWOOMYUNG Then you need to reference of that childByAutoId
Cool, Thanks again !
@KUNWOOMYUNG Check the edited answer for that, but its not the preferred way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.