gpt4 book ai didi

ios - 如何从 Firestore 返回数组中的数据?

转载 作者:行者123 更新时间:2023-11-28 11:29:48 25 4
gpt4 key购买 nike

我正在构建一个 IOS 应用程序,我正处于要在 pickerview 中加载一些类别的阶段。类别列表作为文档存储在 Firestore 中。

我有一个函数可以执行以下操作:

  1. 初始化数组
  2. 从 Firestore 读取数据
  3. 在数组中附加值
  4. 返回数组

问题是函数似乎返回了数组,甚至没有从 Firestore 获得任何结果

我的数据模型

class categoryModel {
var catImg=""
var catName=""
var catID=""
//set categories
init(catImg: String, catName:String, catID:String){
self.catName=catName
self.catImg=catImg
self.catID=catID
}
}

Firestore 引用

struct FirestoreReferenceManager {

static let event = "events"

static let db = Firestore.firestore()
static let rootEvents = db.collection("events")
static let rootUsers = db.collection("users")

}

Firestore 服务

class FirestoreService {

static func getCategoryfromDB()->[categoryModel] {

let rootCategory = FirestoreReferenceManager.rootEvents
var tabCategory=[categoryModel]()


rootCategory.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {

tabCategory.append(categoryModel(catImg: document.data()["img"] as! String, catName:document.data()["name"] as! String, catID: document.documentID))

print("number of categories running: \(tabCategory.count)")
}
}

}
print("number of categories final: \(tabCategory.count)")
return tabCategory
}
}

检查日志显示的内容

umber of categories final: 0
number of categories final: 0
number of categories running: 1
number of categories running: 2
number of categories running: 3
number of categories running: 4
number of categories running: 5
number of categories running: 6
number of categories running: 7
number of categories running: 8
number of categories running: 9
number of categories running: 10
number of categories running: 11
number of categories running: 1
number of categories running: 2
number of categories running: 3
number of categories running: 4
number of categories running: 5
number of categories running: 6
number of categories running: 7
number of categories running: 8
number of categories running: 9
number of categories running: 10
number of categories running: 11

前两行实际上应该是最后一行。

当我从 View Controller 读取 Firestore 时,数组为空。我尝试对数据模型进行硬编码,它工作正常。

我读到有关获取数据的一些“延迟”,但是如何解决这个问题?

我该如何解决这个问题?

最佳答案

像这样的东西应该可以做到。dispatch 是一个可以添加闭包的组。一旦所有这些都完成,就会调用通知闭包,您可以在其中返回数据。阅读调度组,这将有所帮助。

static func getCategoryfromDB(completed: @escaping ([categoryModel]) -> Void) {

let rootCategory = FirestoreReferenceManager.rootEvents
var tabCategory=[categoryModel]()
let dispatch = DispatchGroup()
dispatch.enter()
rootCategory.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {

tabCategory.append(categoryModel(catImg: document.data()["img"] as! String, catName:document.data()["name"] as! String, catID: document.documentID))

print("number of categories running: \(tabCategory.count)")
}
}
dispatch.leave()

}
dispatchGroup.notify(queue: .main, execute: {
print("number of categories final: \(tabCategory.count)")
completed(tabCategory)

})


}

关于ios - 如何从 Firestore 返回数组中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com