Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift FIrebase: moving data to another firebase ref

I have a simple shopping list app backed/synced by a firebase and items added by multiple users. I've created data structs for "GroceryItem" and "Users".

One feature of my app is that you can click the cell and it will put a checkmark next to the item as well as change a bool of "completed" to true.

I'm trying to make a button where it will it will moved all check marked items to a separate list called "history".

Below is one of my many failed attempts at this. I also included the error that XCode is giving me:

'Element' (aka 'AnyObject') is not convertible to 'FDataSnapshot'; did you mean to use 'as!' to force downcast?

@IBAction func itemsBoughtACTION(sender: AnyObject) {
        ref.queryOrderedByChild("completed").observeEventType(.Value,
            withBlock: { snapshot in

        for item in snapshot.children {
            var lastItem = GroceryItem(item)

            }
        })
    }

EDIT: I just want to data some data already stored in firebase, move it to another firebase location and delete the original.

like image 703
Kenny G Avatar asked Dec 07 '25 09:12

Kenny G


2 Answers

The process is: query for the data you want, write it out to another node, and then delete it from the original node.

Your code above won't work as it's expecting to be passed a control from the UI instead of a FDataSnapshot. If you have already done the query and have the dataset, you should create a function that would be passed a FDataSnapshot as a parameter and process it accordingly.

To simplify the answer, assume that you need to get the snapshot and process it when the button is clicked.

There's a lot of different ways to approach this, here's one conceptual option ( untested so don't copy paste)

        //method is called when a button in the UI is clicked/tapped.
        @IBAction func itemsBoughtACTION(sender: AnyObject) {

           let rootRef = Firebase(url:"https://your-Firebase.firebaseio.com")

           let groceryRef = rootRef.childByAppendingPath("groceryLists")

           //get each child node of groceryRef where completed == true
           groceryRef.queryOrderedByChild("completed").queryEqualToValue(true)
       .observeEventType(.ChildAdded, withBlock: { snapshot in

              //set up a history node and write the snapshot.value to it
              // using the key as the node name and the value as the value.
              let historyNode = rootRef.childByAppendingPath("history")
              let thisHistoryNode = historyNode.childByAppendingPath(snapshot.key)
              thisHistoryNode.setValue(snapshot.value) //write to the new node

              //get a reference to the data we just read and remove it
              let nodeToRemove = groceryRef.childByAppendingPath(snapshot.key)
              nodeToRemove.removeValue();

           })

        }
like image 65
Jay Avatar answered Dec 10 '25 01:12

Jay


**Swift 4.2 version of Jay's answer **

let rootRef = Firebase(url:"https://your-Firebase.firebaseio.com")
let groceryRef = ref.child("groceryLists").childByAutoId()

groceryRef.queryOrdered(byChild: "completed").queryEnding(atValue: true)
        .observe(.childAdded) { (snapshot) in
            let historyNode = rootRef.child(snapshot.key)
            thisHistoryNode.setValue(snapshot.value)

            //get a reference to the data we just read and remove it
            let nodeToRemove = nodeToRemove.child(snapshot.key)
            nodeToRemove.removeValue();
    }
like image 42
LizG Avatar answered Dec 09 '25 23:12

LizG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!