简体   繁体   English

iOS Swift FIrebase:将数据移动到另一个firebase引用

[英]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. 我有一个简单的购物清单应用程序由firebase支持/同步,以及多个用户添加的项目。 I've created data structs for "GroceryItem" and "Users". 我已经为“GroceryItem”和“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. 我的应用程序的一个功能是您可以单击单元格,它将在项目旁边添加一个复选标记,并将“已完成”的布尔值更改为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: 我还包括了XCode给我的错误:

'Element' (aka 'AnyObject') is not convertible to 'FDataSnapshot'; 'Element'(又名'AnyObject')不能转换为'FDataSnapshot'; did you mean to use 'as!' 你的意思是用'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. 编辑:我只想对已存储在firebase中的一些数据进行数据处理,将其移动到另一个firebase位置并删除原始数据。

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. 上面的代码无法正常工作,因为它希望从UI而不是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. 如果您已经完成了查询并拥有数据集,则应创建一个函数,该函数将FDataSnapshot作为参数传递并相应地处理它。

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();

           })

        }

**Swift 4.2 version of Jay's answer ** ** Swift 4.2版Jay的答案**

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM