【问题标题】:Firestore - Creating a copy of a collectionFirestore - 创建集合的副本
【发布时间】:2018-11-20 03:22:26
【问题描述】:

所以我有一个名为“草稿”的集合,其中包含多个文档,每个文档都有一个自动 ID。每个文档都包含字段“名称”和“详细信息”。每个文档都显示在“nameLabel”和“detailsLabel”下的 tableViewCell 中。我想做的是,当用户单击第一个 viewController 屏幕顶部的按钮时,将创建集合“草稿”的副本并将其粘贴到名为“消息”的新集合名称下。然后,该集合引用第二个 viewControllers tableViewCells 就像在第一个 ViewController 上一样,只是这次它在集合“消息”下被引用。做了一些研究后,我有一个模糊的倾向,即答案使用云函数来创建集合的副本并将其粘贴到新的集合名称中。但是对于编码和firebase来说相对较新,我不知道如何做到这一点,也不知道这是否是正确的解决方案。请有人帮忙,非常感谢任何帮助!谢谢!

第一个 ViewController

func loadDrafts() {

    let userRef = db.collection("Users").document(user!)

    let draftsRef = userRef.collection("Drafts")

    exercisesRef.getDocuments { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            if let snapshot = querySnapshot {
                for document in snapshot.documents {
                    let data = document.data()
                    let name = data["name"] as? String ?? ""
                    let details = data["details"] as? String ?? ""
                    let newDrafts = DraftMessages(name: name, details: details)
                    self.array.append(newDrafts)
                }
                self.tableView.reloadData()
            }
        }
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DraftsCell

    cell.nameLabel.text = array[indexPath.row].name
    cell.detailsLabel.text = array[indexPath.row].details

    return cell
}

@IBAction func goButton(_ sender: UIButton) {

\\ Add code here to create copy of previous collection "Drafts" and paste in new collection "Messages"

}

第二个视图控制器

func loadData() {

    let userRef = db.collection("Users").document(user!)

    userRef.collection("Messages").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let data = document.data()
                let name = data["name"] as? String ?? ""
                let details = data["details"] as? String ?? ""
                let newMessages = Messages(name: name, details: details)
                self.array.append(newMessages)
            }
            self.tableView.reloadData()
        }
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MessagesCell

    cell.nameLabel.text = array[indexPath.row].name
    cell.detailsLabel.text = array[indexPath.row].details

    return cell

}

【问题讨论】:

  • 这可以在 Swift 或 Cloud Functions 上的 Node.js 中完成(可能还有六种其他技术)。这些都没有比另一个更好。要求我们选择一个是一项技术推荐,这在 Stack Overflow 上是题外话,因为它吸引的是意见而不是事实答案。选择您最熟悉的方法(似乎可能是 Swift),并实现该功能。如果您在此过程中遇到问题,请通过minimal code that reproduces where you got stuck 报告。
  • 嗨弗兰克,非常感谢您的回复。 Swift 绝对是我的首选,但我已经尝试/研究了数周,但没有运气。请你让我开始,因为我真的卡住了?谢谢!
  • 您已经在阅读草稿文件。所以下一步是创建一个按钮单击处理程序,循环相同的文档,然后将它们写入不同的集合。你难倒了什么具体的部分?你能把你的问题限制在那个代码上吗?
  • 创建循环遍历相同文档的按钮单击处理程序让我现在很难过,不幸的是我目前没有代码,因为我真的很难过!你能帮我写代码吗?非常感谢您到目前为止的帮助,真的很有帮助!
  • 嗨弗兰克,经过一段时间的研究,终于弄明白了!再次感谢您的帮助,非常感谢! ??????

标签: swift uitableview firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

这是我的工作解决方案。非常感谢弗兰克斯的帮助!

@IBAction func goButton(_ sender: UIButton) {

    let userRef = db.collection("Users").document(user!)

    let draftsRef = userRef.collection("Drafts")

    draftsRef.getDocuments { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            if let snapshot = querySnapshot {
                for document in snapshot.documents {
                    let data = document.data()
                    let batch = self.db.batch()
                    let docset = querySnapshot

                    let messagesRef = userRef.collection("Messages").document()

                    docset?.documents.forEach {_ in batch.setData(data, forDocument: messagesRef)}

                    batch.commit(completion: { (error) in
                        if let error = error {
                            print("\(error)")
                        } else {
                            print("success")
                        }
                    })
                }
            }
        }
    }
}

为 Vaibhav Jhaveri 编辑:

这个函数(希望)同时复制获取的文档数据和该文档子集合中的数据。 (虽然我没有测试过)

func duplicate() {
    let userRef = db.collection("Users").document(userID)
    let batch = self.db.batch()

    let draftsRef = userRef.collection("Drafts")
    draftsRef.getDocuments { (snapshot, err) in
        if let err = err {
            print(err.localizedDescription)
            return
        }

        guard let snapshot = snapshot else { return }

        snapshot.documents.forEach({ (document) in
            let data = document.data()
            let messageID = UUID().uuidString

            let messagesRef = userRef.collection("Messages").document(messageID)
            batch.setData(data, forDocument: messagesRef, merge: true)

            let yourSubCollectionRef = draftsRef.document(document.documentID).collection("yourSubCollection")
            yourSubCollectionRef.getDocuments(completion: { (subSnapshot, subErr) in
                if let subErr = subErr {
                    print(subErr.localizedDescription)
                    return
                }

                guard let subSnapshot = subSnapshot else { return }

                subSnapshot.documents.forEach({ (subDocument) in
                    let subData = subDocument.data()
                    let subDocID = UUID().uuidString

                    let yourNewSubCollectionRef = userRef.collection("Messages").document(messageID).collection("yourSubCollection").document(subDocID)
                    batch.setData(subData, forDocument: yourNewSubCollectionRef, merge: true)
                })
            })
        })

        batch.commit()
    }
}

【讨论】:

  • 这只是克隆字段,那么克隆文档中存在的集合呢?
  • @VaibhavJhaveri 这是否意味着您想知道该问题的答案?或者你是说我应该在上面的回答中包含这个?
  • 是的,我想知道我的问题的答案。您可以通过包含它来更新您的答案
  • @VaibhavJhaveri 请在上面找到我的答案,注意您需要知道子集合字段的字符串值。希望这会有所帮助
猜你喜欢
  • 2021-11-14
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2018-04-18
  • 2020-12-14
  • 2018-09-14
相关资源
最近更新 更多