Cloud Firestore .getDocuments callback not functioning on Swift5

60 views Asked by At

I am currently building an iOS13 app using Swift5. The database I'm using is Cloud FireStore. The following link is the image of my current collection. Image Of Current FireStore Collection

I am also using FirebaseAuth.

My goal : Remove currently logged in user from all of the rooms the user is currently participating in when applicationWillTerminate. Use .whereField("participants", arrayContains: displayName) to find all documents(also known as "rooms") and remove the current user's displayName from each participating "rooms"s participants array

Current Status and Concern : My code is shown as below. When I terminate my app, print("displayName") prints to the console, but all of the codes inside of db.collection("rooms") seem to be not responsive. print("Hello World!") inside of db.collection("rooms") callback does not print to the console along with the rest of callback code. I'm not too sure why db.collection("rooms") callbacks are not functioning.

    func applicationWillTerminate(_ application: UIApplication) {
        let db = Firestore.firestore()

        if let currentUser = Auth.auth().currentUser {
    
            let displayName = currentUser.displayName!
            
            print("displayName")    // This prints
            
            db.collection("rooms").whereField("participants", arrayContains: displayName)
                .getDocuments { (querySnapshot, error) in
    
                    print("Hello World!") // This does not print, and the code below doesn't seem to respond
                    
                    if let e = error {
                        print (e.localizedDescription)
                    } else {
                        if let snapshotDocuments = querySnapshot?.documents {
                            for doc in snapshotDocuments {
                                let data = doc.data()
                                print(data)
                            }
                        }
                    }
                } // db.collection("rooms")
            } else {
                print("No Current User Signed In")  // This does not print
            }
        print ("End of applicationWillTerminate")   // This prints
    }
0

There are 0 answers