iOS Swift FIrebase:将数据移动到其他Firebase参考

我有一个简单的购物清单应用程序支持/同步的firebase和由多个用户添加的项目。 我已经为“GroceryItem”和“Users”创build了数据结构。

我的应用程序的一个特点是,你可以点击单元格,它会把一个复选标记旁边的项目以及更改为“完成”布尔为true。

我试图做一个button,它会将所有检查标记的项目移动到一个单独的名单“历史”。

下面是我在这个失败的尝试之一。 我还包括了XCode给我的错误:

“元素”(又名“AnyObject”)不能转换为“FDataSnapshot”; 你的意思是使用“as!” 强迫低调?

@IBAction func itemsBoughtACTION(sender: AnyObject) { ref.queryOrderedByChild("completed").observeEventType(.Value, withBlock: { snapshot in for item in snapshot.children { var lastItem = GroceryItem(item) } }) } 

编辑:我只想要数据已经存储在firebase中的一些数据,将其移动到另一个firebase位置,并删除原来的。

该过程是:查询所需的数据,将其写入另一个节点,然后将其从原始节点中删除。

上面的代码将无法正常工作,因为它期望从UI传递控件而不是FDataSnapshot。 如果您已经完成了查询并获得了数据集,则应该创build一个函数,该函数将传递一个FDataSnapshot作为参数并相应地进行处理。

为了简化答案,假设您需要获取快照并在点击button时处理快照。

有很多不同的方法来处理这个,这里有一个概念选项(未经testing,所以不要复制粘贴)

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