Untitled

mail@pastecode.io avatar
unknown
swift
4 years ago
3.0 kB
4
Indexable
Never
  func getQuickDisposals(binID: String, completion: @escaping ([QuickDisposal]) -> Void) {
        let quickDisposalDispatchGroup = DispatchGroup()
        let usersDispatchGroup = DispatchGroup()
        var quickDisposals: [QuickDisposal] = []
        var uids = Set<String>()
        var arrayuids: [String] = []
        var profilePictures: [String: String] = [:]
        var usernames: [String: String] = [:]
        
        quickDisposalDispatchGroup.enter()
        database.collection("quickDisposalSessions")
            .whereField("binID", isEqualTo: binID)
            .limit(to: 10)
            .getDocuments() { (querySnapshot, error) in
                if let error = error {
                    print("Error getting documents: \(error)")
                } else {
                    for document in querySnapshot!.documents {
                        guard var model = try? FirestoreDecoder().decode(QuickDisposal.self, from: document.data()) else { continue }
                        model.id = document.documentID
                        quickDisposals.append(model)
                    }
                }
                quickDisposalDispatchGroup.leave()
            }
          
        usersDispatchGroup.enter()
        quickDisposalDispatchGroup.notify(queue: .main) {
            for disposal in quickDisposals {
                guard let uid = disposal.userID else { continue }
                uids.insert(uid)
            }
            for value in uids {
                arrayuids.append(value)
            }
        
            self.database.collection("users").whereField("uid", in: arrayuids).getDocuments() { (querySnapshot, error) in
                if let error = error {
                    print("Error getting documents: \(error)")
                } else {
                    for document in querySnapshot!.documents {
                        if let username = document.get("username") as? String {
                            usernames[document.documentID] = username
                        } else {
                            usernames[document.documentID] = "anonymous"
                        }
                        
                        if let profilePicture = document.get("profilePicture") as? String {
                            profilePictures[document.documentID] = profilePicture
                        } else {
                            profilePictures[document.documentID] = nil
                        }
                    }
                }
                usersDispatchGroup.leave()
            }
        }
        
        usersDispatchGroup.notify(queue: .main) {
            for index in quickDisposals.indices {
                quickDisposals[index].username = usernames[quickDisposals[index].userID!]
                quickDisposals[index].profilePicture = profilePictures[quickDisposals[index].userID!]
                completion(quickDisposals)
            }
            
        }
        
    }