[英]How to enter a DispatchGroup in a firebase firestore callback
I have this function where I want to play an animation until the fetching of my data is complete.我有这个 function 我想在其中播放 animation 直到我的数据获取完成。 I have this variable "loadFetchDone" to indicate that.
我有这个变量“loadFetchDone”来表明这一点。 I need to use snapshotListener when I fetch my data from firebase.
当我从 firebase 获取数据时,我需要使用 snapshotListener。
func getLoads(completion: @escaping () -> ()){
bookedLoadsListener = db.collectionGroup("allLoads").whereField("cv", isEqualTo: 1).whereField("status", isEqualTo: "Booked").order(by: "DateBooked", descending: false).addSnapshotListener{ (snapshot, err) in
if err != nil{print("Error getting docs in TruckTrackingView.swift: \(err)")}
loadFetchDone = false
loads = []
let lGroup = DispatchGroup()
for load in snapshot?.documents ?? []{
lGroup.enter()
...
lGroup.leave()
}
}
}
lGroup.notify(queue: .main){
completion()
}
}
}
I use this function to get the loads.我用这个 function 来获取负载。 Once it completes, I set loadFetchDone to true which then stops the loading animation.This is how I call the function
完成后,我将 loadFetchDone 设置为 true,然后停止加载 animation。这就是我调用 function 的方式
.onAppear(){
globalGroup.enter()
getLoads {
globalGroup.leave()
}
globalGroup.notify(queue: .main){
loadFetchDone = true
}
}
If this was a normal getDocuments call, then there wouldn't be a problem but since I am using snapshotListeners, if a change in the document is made, The globalGroup is never entered.如果这是一个普通的 getDocuments 调用,那么不会有问题,但由于我使用的是 snapshotListeners,如果对文档进行了更改,则永远不会输入 globalGroup。 This then results in the EXC BAD INSTRUCTION since I leave the group without ever entering it.
然后这会导致 EXC BAD INSTRUCTION,因为我没有进入该组就离开了该组。 I have tried entering the group inside the snapshotListener but It never notifies the globalGroup.
我尝试在 snapshotListener 中输入组,但它从不通知 globalGroup。
Then simply move the dispatch logic out of the view's lifecycle and into the database return, which is how you should handle it anyway IMO.然后只需将调度逻辑移出视图的生命周期并进入数据库返回,这就是 IMO 无论如何处理它的方式。 Do all of your entering and exiting within the Firestore return closure.
在 Firestore 返回关闭范围内完成所有进出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.