【问题标题】:iOS Swift FIrebase: moving data to another firebase refiOS Swift FIrebase:将数据移动到另一个 firebase ref
【发布时间】:2016-03-21 23:20:12
【问题描述】:

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

我的应用程序的一个功能是,您可以单击单元格,它会在项目旁边放置一个复选标记,并将“已完成”的布尔值更改为 true。

我正在尝试制作一个按钮,它将所有选中标记的项目移动到一个名为“历史”的单独列表中。

以下是我在这方面的许多失败尝试之一。我还包括 XCode 给我的错误:

'Element'(又名'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 位置并删除原始数据。

【问题讨论】:

  • 你是从代码中调用这个函数并传递给它一个快照吗?
  • 我是 firebase 新手,我的理解是快照是做到这一点的唯一方法。我被误导了吗?
  • 您描述的错误表明您可能正在从代码中调用 itemsBoughtACTION 并将其传递给 FDataSnapshot。如果是这样,你不想这样做。 itemsBroughtAction 看起来像连接到 UI 中的 NSButton ,因此按下该按钮会将按下的 NSButton 传递给该方法。使用该方法后,您可以创建查询、获取快照并处理数据。让我们知道情况如何.. 我的猜测可能会离谱。
  • 这是正确的。我有一个带有数据的当前 firebase 数据模型。当按下连接到 itemsBoughtACTION 的按钮时,我希望将列表中具有“已完成”属性且值为 true 的所有项目保存在单独的 Firebase 列表中。

标签: ios swift firebase


【解决方案1】:

过程是:查询你想要的数据,写出到另一个节点,然后从原来的节点中删除。

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

为简化答案,假设您需要获取快照并在单击按钮时对其进行处理。

有很多不同的方法来解决这个问题,这里有一个概念选项(未经测试,所以不要复制粘贴)

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

           })

        }

【讨论】:

  • 函数的第 2 行到最后一行:让 nodeToRemove =groceryRef.childByAppendingValue(snapshot.key) xcode 说“Firebase”类型的值没有成员“childByAppendingValue”
  • 另外,非常感谢您的帮助。它的工作原理减去删除旧的参考,但这是一个很好的开始......谢谢你
  • 正如我所说,不要复制粘贴!它应该是 .childByAppendingPath。答案已更新。您还可以将节点值设置为 nil,这也会将其删除。如果我的回答有帮助,请采纳。
  • 工作完美,已接受答案,谢谢您的帮助。
【解决方案2】:

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

【讨论】:

  • 太棒了!谢谢你把它放在一起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 2017-09-27
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多