gpt4 book ai didi

ios - Firebase 查询在错误的时间运行

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:24 28 4
gpt4 key购买 nike

由于某种原因,当我运行以下方法时,第一个查询在该方法完成后运行。我尝试使用分派(dispatch) block 来强制查询先运行,但查询根本不会运行,应用程序只是卡住。如果您知道哪里出了问题,请告诉我。

没有调度组的方法:

func loadConversations() {
let ref = FIRDatabase.database().reference()
let convoRef = ref.child("users").child(FIRAuth.auth()!.currentUser!.uid).child("conversations")
var conversationID = [String]()
print(1)
convoRef.queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
let enumerator = snapshot.children
print(2)
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
print(3)
if let id = rest.value as? String{
conversationID.append(id)
print(id)
}
}
})
print(4)
print("size: \(conversationID.count)")
for id in conversationID {
print(5)
ref.child("conversations").queryEqual(toValue: id).observeSingleEvent(of: .value, with: { (snapshot) in
print(6)
if let convo = snapshot.value as? [String : AnyObject] {
print(7)
let conversation = Conversation()
conversation.conversationID = id
conversation.name = "Temporary test name"
self.Conversations.append(conversation)
}
})
ref.removeAllObservers()
}
print(8)
self.conversationTableView.reloadData()
ref.removeAllObservers()

}

这打印:

1
4
size: 0
8
2
3
-KZyMMzXmkQC_OF0T08_

与调度组:

func loadConversations() {
let dispatchGroup = DispatchGroup()
let ref = FIRDatabase.database().reference()
let convoRef = ref.child("users").child(FIRAuth.auth()!.currentUser!.uid).child("conversations")
var conversationID = [String]()
print(1)


dispatchGroup.enter()
convoRef.queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

let enumerator = snapshot.children
print(2)
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
print(3)
if let id = rest.value as? String{
conversationID.append(id)
print(id)
dispatchGroup.leave()
}
}


})

print(4)
dispatchGroup.wait()
print("size: \(conversationID.count)")

for id in conversationID {
print(5)
ref.child("conversations").queryEqual(toValue: id).observeSingleEvent(of: .value, with: { (snapshot) in
print(6)
if let convo = snapshot.value as? [String : AnyObject] {
print(7)
let conversation = Conversation()
conversation.conversationID = id
conversation.name = "Temporary test name"
self.Conversations.append(conversation)
}
})
}
print(8)
self.conversationTableView.reloadData()
ref.removeAllObservers()
}

这打印

1
4

但随后它只是卡住并等待。查询永远不会运行。

我不确定为什么查询似乎没有被输入。输入查询时,它工作得很好,但输入得太晚了。任何帮助是极大的赞赏。谢谢!

最佳答案

这仅仅是因为 Firebase 查询是在后台线程上执行的,因为它本质上是一个网络调用。因此响应在您的方法完成后出现,否则 UI 将被阻塞,直到响应来自 Firebase

您需要在查询响应中编写一个闭包,以便在获得响应后立即执行一段代码。

func loadConversations(completion:@escaping (Array<String>) -> Void) -> Void {
let ref = FIRDatabase.database().reference()
let convoRef = ref.child("users").child(FIRAuth.auth()!.currentUser!.uid).child("conversations")
var conversationID = [String]()
print(1)
convoRef.queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
let enumerator = snapshot.children
print(2)
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
print(3)
if let id = rest.value as? String{
conversationID.append(id)
print(id)
}
}
completion(conversationID)
})
}

这会将您的调用发送回从内部调用的任何地方

loadConversations { (array) in

//do something with this value and execute next query
}

关于ios - Firebase 查询在错误的时间运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41354457/

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