iOS Swift FIrebase: moving data to another firebase ref

Viewed 2469

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.

2 Answers

**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();
    }
Related