gpt4 book ai didi

ios - 从另一个 "getDocument"查询中的 "getDocument"查询中检索数据

转载 作者:行者123 更新时间:2023-11-28 07:39:07 30 4
gpt4 key购买 nike

我的代码从 Firestore 检索数据时遇到问题。

我的代码中有 2 个类:Exercise 和 Tag。我的 FirestoreDatabase 中有 2 个集合:练习和标签

我需要从“练习”集合中提取所有“练习”文档。每个“练习”文档都有一个名为“标签”的字段,它是一个字符串数组。数组的每个字符串都包含“id”,它指的是“tag”在“tags”集合中的文档。因此,在“tags”集合中查询此 id,使我能够获得正确的“tag”文档并访问其所有数据。这正是我想在我的代码中做的。

我需要将所有练习都放入一个练习对象中,为此我必须在另一个 getDocument 查询中使用一个 getDocument 查询,以便从“标签”集合中获取练习的“标签”

这是我的类(class)标签和练习:

class Tag {  
var id: String?
var type: String?
var description: String?

init(id: String, type: String, description: String) {
self.id = id
self.type = type
self.description = description
}
}

class Exercise {
let id: String?
let group: String?
let tags: [Tag]
let title : String!

init(id: String, group: String, tags: [Tag], title: String){
self.id = id
self.group = group
self.tags = tags
self.title = title
}

}

这是我从 Firestore 数据库中获取“练习”的代码:

func fetchExercises(completion: @escaping ([Exercise]) -> ()) {

let exercisesRef = Firestore.firestore().collection("exercises")

exercisesRef.getDocuments() { (querySnapshot, err) in

var exercisesArray = [Exercise]()

if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
//print("\(document.documentID) => \(document.data())")

let myData = document.data()
let exercise_ID = document.documentID
let exercise_group = myData["Group"] as! String
let tagsArray = myData["Tags"] as! [String]

var exercise_tags: [Tag] = [Tag]()

for tag in tagsArray {
let tagID: String = tag
fetchTagfromID(tagID: tagID) { (tag: Tag) in
exercise_tags.append(tag)
}
}

let exercise_title = myData["Title"] as! String

exercisesArray.append(Exercise(id: exercise_ID,
group: exercise_group,
tags: exercise_tags,
title: exercise_title,
))
}
DispatchQueue.main.async{
print("EXERCISE FETCH HAS FINIS")
completion(exercisesArray)
}
}
}
}


func fetchTagfromID(tagID: String, completion: @escaping (Tag) -> ()) {


let tagRef = Firestore.firestore().collection("tags").document(tagID)

tagRef.getDocument() { (document, err) in

if let err = err {
print("Error getting documents: \(err)")
} else {

let myData = document?.data()

let tagDescription: String = myData!["description"] as! String
let tagType: String = myData!["type"] as! String
let tag: Tag = Tag(id: tagID, type: tagType, description:
tagDescription)


DispatchQueue.main.async{
print("TAGS FETCH HAS FINISHED")
completion(tag)
}
}
}
}

我的问题来自执行时间(队列)代码。

我需要先填写“exercise_tags”(辅助 getDocument 查询),然后继续并完成 fetchExercise(主 getDocument 查询),但 Firestore 不允许(或不知道如何)这样做。代码首先完成主 getDocument 查询 (fetChExercises),然后返回完成辅助 getDocument 查询 (fetchTagfromID)。

总而言之,我需要在运行时获取此日志:

TAGS FETCH HAS FINISHED
EXERCISES FETCH HAS FINISHED

现在我得到了相反的结果。

你们知道如何解决这个问题吗?也许改变调度队列...

我现在如何分两步解决这个问题,但优雅的解决方案是一步完成所有事情。这是,fetchExercises。

谢谢!

最佳答案

所以您需要做的是获取一个练习的所有标签,然后获取另一个练习。并且在所有 fetch 调用完成处理程序之后更新 UI。我对代码做了一些更改,您可以检查一下。

   var exercisesArray = [Exercise]()
var listFromFetchExercise = []() //it will contain all object of array
querySnapshot!.documents. set DataType according to that.
var completion_exercises_Listner: () -> ()

func fetchExercises(completion: @escaping ([Exercise]) -> ()) {

let exercisesRef = Firestore.firestore().collection("exercises")

exercisesRef.getDocuments() { (querySnapshot, err) in

if let err = err {
print("Error getting documents: \(err)")
} else {

listFromFetchExercise = querySnapshot!.documents
fetchAllExercise()

DispatchQueue.main.async{
print("EXERCISE FETCH HAS FINIS")
completion(exercisesArray)
}
}
}
}

func fetchAllExercise(completion: @escaping ([Exercise]) -> ()){

fetchExercise()
completion_exercises_Listner = {
completion(exercisesArray);
}

}

func fetchExercise(index:Int = 0) {

let document = listFromFetchExercise[index]

let myData = document.data()
let exercise_ID = document.documentID
let exercise_group = myData["Group"] as! String
let tagsArray = myData["Tags"] as! [String]

var exercise_tags: [Tag] = [Tag]()

fetchTagsFromIDS(tagsArray) { (tag: [Tag]) in
exercise_tags.append(contentsOf: tag)

let exercise_title = myData["Title"] as! String

exercisesArray.append(Exercise(id: exercise_ID,
group: exercise_group,
tags: exercise_tags,
title: exercise_title,

DispatchQueue.main.async{
exercisesArray.append(tag)
if (index + 1) < exercisesArray.count {
fetchExercise(index+1,)
}else{
//Done
completion_exercises_Listner()
}

}
))

}



var listofTags = [String]()
var resultofTags = [Tag]()

func fetchTagsFromIDS(tagIDS:[String],completion: @escaping (_ tags:[
String]) -> ()){

listofTags = tagIDS;
fetchTagfromID() //Start With first tag

completionListner = {
completion(resultofTags);
}

}

var completionListner: () -> ()

func fetchTagfromID(index:Int = 0) {

let tagRef = Firestore.firestore().collection("tags").document(tagID)

tagRef.getDocument() { (document, err) in

if let err = err {
print("Error getting documents: \(err)")
} else {

let myData = document?.data()

let tagDescription: String = myData!["description"] as! String
let tagType: String = myData!["type"] as! String
let tag: Tag = Tag(id: tagID, type: tagType, description:
tagDescription)


DispatchQueue.main.async{
print("TAGS FETCH HAS FINISHED")
resultofTags.append(tag)
if (index + 1) < listofTags.count {
fetchTagfromID(index+1,)
}else{
//Done
completionListner()
}
//completion(tag)
}
}
} }

关于ios - 从另一个 "getDocument"查询中的 "getDocument"查询中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757171/

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