Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI freezes during decoding #107

Open
jscottanderson opened this issue Jun 17, 2021 · 0 comments
Open

UI freezes during decoding #107

jscottanderson opened this issue Jun 17, 2021 · 0 comments

Comments

@jscottanderson
Copy link

I'm fetching data from Firestore, mapping the documents and decoding each of them using FirestoreDecoder. However, decoding the documents momentarily freezes the UI. Running the code on the background thread makes no difference. How can I prevent the UI from freezing during the decoding?

let collection = Firestore.firestore().collection("roll_groups")

collection.addSnapshotListener { (snapshot, error) in
    if let error = error {
        print("Error fetching roll groups: \(error.localizedDescription)")
    } else if let snapshot = snapshot {
        DispatchQueue.global(qos: .background).async {
            let rollGroups = snapshot.documents.map { doc -> RollGroup? in
                do {
                    let rollGroup = try FirestoreDecoder().decode(RollGroup.self, from: doc.data())
                    return rollGroup
                } catch {
                    print("Error decoding roll groups: \(error)")
                    return nil
                }
            }
            
            DispatchQueue.main.async {
                completion(rollGroups)
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant