【问题标题】:FireStore Setting Doc Title by document count + 1FireStore 按文档计数 + 1 设置文档标题
【发布时间】:2021-12-29 20:23:00
【问题描述】:

请查看我的 Firestore 结构

我想将我的文档标题设置为连续的。 我的结构为 [Table] - Drinks - ["Drink (sequence)"] 我的第一个问题:我的文档标题确实遵循顺序,例如“饮料 1”和“饮料 2”等,但是当我将新饮料添加到另一张桌子时 - 新文件从饮料 3 而不是饮料 1 开始。

我的第二个问题:如果有“drink 1”和“drink 2 文件”,如果我删除它们,那么从饮料 2 开始重新创建文件标题。 不知道怎么回事,请帮帮我。

我这样做的代码是

    var drinkCount : Int = 1
     func generateDrinkTitle(table: String,complete:@escaping() -> Void) {
        let docRef = db.collection("Active Table").document(table).collection("Drinks")
        
        docRef.getDocuments { querySnapshot, error in
            if let err = error {
                print("Error in getting: \(err.localizedDescription)")

            } else {
                self.drinkCount = 1
                for doc in querySnapshot!.documents {
                    self.drinkCount += 1
                }
            }
        }

        complete()
    } 

我的整个代码

class OrderViewModel : ObservableObject {
var drinkCount : Int = 1

    func saveDrinks(table : String) {
        var drinks : [[String : Any]] = []
        var drinkextra : [[String: Any]] = []
        for j in cartDrinks {
            for i in j.extra {
                drinkextra.append(["extra": i.extra,"price": i.price])
                drinks.append(["Name":j.name,
                               "Unit":j.unit ,
                               "Price":j.price,
                               "Extra": drinkextra
                              ])
                drinkextra.removeAll()
            }
        }
        db.collection("Active Table").document("Table \(table)").collection("Drinks").document("Drink \(self.drinkCount)").setData(["Drinks": drinks]){ error in
            if let error = error {
                print(error.localizedDescription)
                
            } else {
            }
        }
    }

    func generateDrinkTitle(table: String,complete:@escaping() -> Void) {
        let docRef = db.collection("Active Table").document(table).collection("Drinks")
        
        docRef.getDocuments { querySnapshot, error in
            if let err = error {
                print("Error in getting: \(err.localizedDescription)")

            } else {
                self.drinkCount = 1
                for doc in querySnapshot!.documents {
                    self.drinkCount += 1
                }
            }
        }

        complete()
    }

    func FireUpload(table: String){
        db.collection("Active Table").document("Table \(table)").setData(["Time" : Timestamp(date: Date())]) { error in
            if let error = error {
                print(error.localizedDescription)
                return
            }
        }
        if !mealDeal.mainCourse.isEmpty {
            self.SaveMealDeal(table: table)
        }
        if !cartDrinks.isEmpty {
            generateDrinkTitle(table: "Table \(table)") {
                self.saveDrinks(table: table)
                self.drinkCount = 1
            }
        }
        if !cartexpress.isEmpty {
            self.SaveAlacarte(table: table)
        }

    }
}

【问题讨论】:

  • "我想将我的文档标题设置为顺序" 顺序文档 ID 在 Firestore(和大多数 NoSQL 数据库)中是一种反模式,会影响您的应用程序的性能。你想要这个的原因是什么?
  • 您可以添加您的整个代码以供理解吗?
  • 嗨@FrankvanPuffelen,我想设置这个,以便我的员工可以轻松识别他们添加的最新饮料轮次,然后他们可以选择删除它们或将它们移动到其他表集合,因为选择表不正确或客户取消订单等。
  • @SiddharthPatel 我现在已经在我的帖子中包含了全部内容。非常感谢
  • 任何顺序数据都应该在字段中,而不是文档 ID - 您可以按字段查询。文档 ID 在保持文档唯一性方面非常有用 - 它们在实际使用中几乎没有任何功能(强调“几乎”)

标签: swift firebase google-cloud-firestore


【解决方案1】:

我接受了 cmets 中提出的反击想法;非常感谢你帮助我。这是我的计数器 id 代码:

docRef.addSnapshotListener { documentSnapshot, error in
            guard let document = documentSnapshot?.exists else {return}
            if document {
                
                self.db.collection("Active Table").document(table).getDocument { documentSnapshot, error in
                    if let document = documentSnapshot,((documentSnapshot?.exists) != nil) {
                        let dataDescription = document.data()
                        let Dcounter = dataDescription?["DrinkCounter"] as? Int ?? 0
                        let Acounter = dataDescription?["AlacarteCounter"] as? Int ?? 0
                        self.alacarteCounter = Acounter
                        self.drinkCount = Dcounter
                    }
                }
            } else {
                print("No document")
                self.alacarteCounter = 0
                self.drinkCount = 0
                self.db.collection("Active Table").document(table).setData(["DrinkCounter": 0 as Int,"AlacarteCounter": 0 as Int,"MealDealCounter": 0 as Int,"Time": Timestamp(date: Date())])
            }
        }

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2015-11-14
    • 1970-01-01
    • 2021-04-26
    • 2021-12-04
    相关资源
    最近更新 更多