iOS Swift FIrebase:将数据移动到另一个firebase ref [英] iOS Swift FIrebase: moving data to another firebase ref
问题描述
我有一个简单的购物清单应用程序由firebase支持/同步,以及多个用户添加的项目。我已经为GroceryItem和Users创建了数据结构。
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".
我的应用程序的一个功能是你可以单击单元格,它会在旁边放置一个复选标记该项目以及将已完成的bool更改为true。
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".
以下是我多次失败的尝试之一。我还包括了XCode给我的错误:
Below is one of my many failed attempts at this. I also included the error that XCode is giving me:
'Element'(又名'AnyObject')不能转换为'FDataSnapshot';您的意思是使用强制垂头丧气的
@IBAction func itemsBoughtACTION(sender: AnyObject) {
ref.queryOrderedByChild("completed").observeEventType(.Value,
withBlock: { snapshot in
for item in snapshot.children {
var lastItem = GroceryItem(item)
}
})
}
编辑:我只想数据已经存储在火力一些数据,其移动到另一个火力点位置,并删除原
I just want to data some data already stored in firebase, move it to another firebase location and delete the original.
推荐答案
过程是:查询您想要的数据,将其写入另一个节点,然后将其从原始节点中删除。
The process is: query for the data you want, write it out to another node, and then delete it from the original node.
您的代码胜过'因为它期望从UI而不是FDataSnapshot传递控件。如果您已完成查询并拥有数据集,则应创建一个函数,该函数将FDataSnapshot作为参数传递并相应地处理它。
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();
})
}
这篇关于iOS Swift FIrebase:将数据移动到另一个firebase ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!