Firestore - 创建集合的副本

时间:2018-06-10 21:25:02

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

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

First 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"

}

第二个ViewController

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

}

1 个答案:

答案 0 :(得分:2)

这是我的工作解决方案。非常感谢Franks的帮助!

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