简体   繁体   English

如何在 firebase firestore 回调中输入 DispatchGroup

[英]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.

相关问题 Swift-与DispatchGroup()异步从Firebase Firestore下载数据时出现问题 - Swift - Problem While Downloading Data From Firebase Firestore Asynchronously with DispatchGroup() Swift DispatchGroup导致错误:[Firebase / Firestore] [I-FST000001]无法到达Cloud Firestore后端 - Swift DispatchGroup causes error: [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend 立即调用 DispatchGroup notify() - 不等待进入/离开 - DispatchGroup notify() called immediately - not waiting for Enter/Leave Firestore:使用SnapshotListener调用异步函数,并在DispatchGroup循环中导致崩溃 - Firestore: Calls with of async function with SnapshotListener and in a cycle with DispatchGroup cause a crash 如何在一段时间后停止DispatchGroup? iOS版 - How to stop DispatchGroup after time? iOS Swift 中 Firebase 实时数据库观察方法中具有异步函数的 DispatchGroup - DispatchGroup with async functions in a Firebase Realtime DB observe method in Swift Firebase function 没有在带有 DispatchGroup 的 forEach 循环中被调用 - Firebase function not getting called inside a forEach loop with a DispatchGroup Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 如何从Firebase Cloud Firestore提取数据 - How to extract data from Firebase Cloud Firestore 如何让VIP用户在firebase和firestore中可写 - How to make VIP users writable in firebase and firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM