askaale askaale - 5 months ago 27
Swift Question

Swift/Firebase copy path

Basically I am struggling with copying paths to other destinations. I have a directory with root/users/id/info, like this:

users{
id{
username:Andreas
dob:12/12/2012
hobby:football
}
}


However, what I want to accomplish, is to move the path with the ID (that contains all the information about andreas, to another path
(root/HighestRanking/)
so it looks like this:
(root/HighestRanking/andreasUID/info)
, but I can't seem to find a way to do so. Any ideas?

Thanks in advance..

Here is the code I currently have:

ref.child("Users").child(uid).observeEventType(FIRDataEventType.ChildAdded) { (snap:FIRDataSnapshot) in
var pathway = snap
self.ref.child("HighestRanking").child(uid)..setValue([pathway], withCompletionBlock: { (error, ref) in
if error != nil {
print("An error occured.")
} else {
print("Success.")
}
}


//An error occurs.. Tried several other methods as well..

Answer

What you might be looking for is something like the following:

ref.child("Users").child(uid).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
  if let data = snapshot.value as! [String : AnyObject] {
    self.ref.child("HighestRanking").child(uid).setValue(data)
  }
})

Observe that since you want to "copy" the data when a UIButton is triggered you should be using FIRDataEventType.Value instead of FIRDataEventType.ChildAdded.

Comments