dispatchgroup executes task in different order in testflight compared to simulator

So my goal is to have congruent functionality both on the iOS simulator in Xcode and as well as a physical device on TestFlight. So currently, I have a function that handles refunds in my app. On the simulator the function runs perfectly fine in the order I expect it to, but the print statements execute in the wrong order which I'm assuming is the reason for misbehaviour on TestFlight simulations.

Here is the method:

@IBAction func cancelPurchasePressed(_ sender: UIButton) {
    guard let nameOfEvent = selectedEventName else { return }
    guard let user = Auth.auth().currentUser else { return }

    
    let alertForCancel = UIAlertController(title: "Cancel Purchase", message: "Are you sure you want to cancel your purchase of a ticket to \(nameOfEvent)? You will receive full reimbursement of what you paid within 5 - 10 days.", preferredStyle: .alert)
    
    
    let cancelPurchase = UIAlertAction(title: "Cancel Purchase", style: .default) { (purchaseCancel) in
        
        self.viewPurchaseButton.isHidden = true
        self.cancelPurchaseButton.isHidden = true
        self.refundLoading.alpha = 1
        self.refundLoading.startAnimating()
        
        self.makeRefundRequest()
        
       
        DispatchQueue.main.asyncAfter(deadline: .now()+1) {
            let group = DispatchGroup()
            self.db.collection("student_users/\(user.uid)/events_bought/\(nameOfEvent)/guests").getDocuments { (querySnapshot, error) in
                guard error == nil else {
                    print("The guests couldn't be fetched.")
                    return
                }
                guard querySnapshot?.isEmpty == false else {
                    print("The user did not bring any guests.")
                    return
                }
                for guest in querySnapshot!.documents {
                    let name = guest.documentID
                    group.enter()
                    self.db.document("student_users/\(user.uid)/events_bought/\(nameOfEvent)/guests/\(name)").delete { (error) in
                        guard error == nil else {
                            print("The guests couldn't be deleted.")
                            return
                        }
                        print("Guests deleted with purchase refund.")
                        group.leave()
                    }
                }
            }
            group.notify(queue: .main) {
                
                self.db.document("student_users/\(user.uid)/events_bought/\(nameOfEvent)").delete { (error) in
                    guard error == nil else {
                        print("Error trying to delete the purchased event.")
                        return
                    }
                    print("The purchased event was succesfully removed from the database!")
                }
                self.refundLoading.stopAnimating()
                self.refundLoading.alpha = 0
                self.ticketFormButton.isHidden = false
                self.cancelPurchaseButton.isHidden = true
                self.viewPurchaseButton.isHidden = true
            }
        }
    }
    
    alertForCancel.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    alertForCancel.addAction(cancelPurchase)
    present(alertForCancel, animated: true, completion: nil)
}

Basically what I have going on is a simple refund request being made to Stripe and a second after I have an asyncAfter code block with some database cleanup in it. I have to do the asyncAfter or else the refund request gets beat out by the other async tasks by speed.

So I took my knowledge of DispatchGroups and decided to implement it since I have an async task in a for loop that I need to be completed before every other task. So I expected this to work fine, despite the order of the print statements being incorrect, but when I ran the exact block of code on my phone via TestFlight, I made a refund and the cell was still showing up in the tableview, meaning the document wasn't deleted from the database properly.

I've been having some terrifying experience recently with DispatchGroups and TestFlight and I just honestly hope to fix all this and have these problems come to an end temporarily. Any suggestions on how I can fix this method to prevent incorrect order on TestFlight?



Read more here: https://stackoverflow.com/questions/67388994/dispatchgroup-executes-task-in-different-order-in-testflight-compared-to-simulat

Content Attribution

This content was originally published by dante at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: