【问题标题】:Read contents of an array out of a Firebase / Firestore query [duplicate]从 Firebase / Firestore 查询中读取数组的内容 [重复]
【发布时间】:2021-02-27 02:43:24
【问题描述】:

大家好,我正在使用 Firebase - Firestore 来保存数据。我刚刚创建了一个数据库调用,它返回一个Date,我从中提取小时分钟

小时和分钟必须保存在带有元组的array 中。

一切似乎都正常,但是当我在 clousure 之外阅读我的 array 时,它的值始终为零。

我还需要在 Firestore 查询方法之外读取array 的内容..

谁能帮我解决这个问题?


private var timeArray: [(Int, Int)] = []

private func getReservations() -> Void {

    Service.Database.reservationRoot.whereField("date", isGreaterThan: Date()).getDocuments() { (querySnapshot, error) in
        
        guard error == nil else  { fatalError("Errore durante il recupero delle prenotazioni \(error!.localizedDescription)") }

        for document in querySnapshot!.documents {
            
            if let timestamp = document["date"] as? Timestamp {
                let hour = timestamp.dateValue().component(.hour)
                let minute = timestamp.dateValue().component(.minute)
                
                self.timeArray.append((hour, minute))

                // The array count is 2
                print(self.timeArray.count)
            }
        }
    }
   
    // The array count is 0
    print(self.timeArray.count)
}

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore


    【解决方案1】:
    for document in querySnapshot!.documents {
            
            if let timestamp = document.data()["date"] as? Timestamp {
                let hour = timestamp.dateValue().component(.hour)
                let minute = timestamp.dateValue().component(.minute)
                
                self.timeArray.append((hour, minute))
                                
            }
        }
    

    你能试试这个代码吗

    【讨论】:

    • 没有,但问题是数组的内容只能在 for cicle for document in querySnapshot!.documents 内部读取,而不能在外部读取。我需要读取for cicle for document in querySnapshot!.documents之外的数组值
    猜你喜欢
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    相关资源
    最近更新 更多