2
\$\begingroup\$

I have this function where it queries through the database and returns a random user if the requirement is met if not then try it again. It works and all but I feel like the code could be better/shorter/faster but I'm running a blank.

It's all done through Firestore, where the Serivces.crud.reads() is a Firestore .getDocuments() function. At the end a list of n number of users is returned. I've tested it and it works but there could be a possibility of checking for the same value twice which means the second time it will accept the user since I'm just assuming it's going to be different, since it's a random generated number.

I just want to remove any possibility of getting the current user in the list of users being returned. If I have to re-write my Services.crud.reads() function, I will.

func reads(query: Query, onSuccess: @escaping (QuerySnapshot) -> Void, onFailure: @escaping (String) -> Void) {
    query.getDocuments { (snapshot, error) in
        if let e = error {
            onFailure(#function + ": " + e.localizedDescription)
        } else {
            if let s = snapshot {
                onSuccess(s)
            }
        }
    }
}

func randomUsers(numOfUsers: Int, onSuccess: @escaping ([DocumentReference]) -> Void) {
    var users: [DocumentReference] = []

    for _ in 0...numOfUsers {
        Services.crud.reads(
            query: self.collection.whereField("quickID", isGreaterThanOrEqualTo: "\(UInt64.random(in: UInt64.min ... UInt64.max))").limit(to: 1),
            onSuccess: { (snapshot) in
            if snapshot.count > 0 {
                let userID = snapshot.documents.first!.documentID

                if cUser.userID == userID {
                    Services.crud.reads(query: self.collection.whereField("quickID", isLessThanOrEqualTo: "\(UInt64.random(in: UInt64.min ... UInt64.max))").limit(to: 1), onSuccess: { (snapshot) in
                        let userID = snapshot.documents.first!.documentID
                        if users.count == numOfUsers {
                            onSuccess(users)
                        } else {
                            users.append(self.collection.document(userID))
                            print("added")
                            return
                        }
                    }) { (e) in
                        print(e)
                    }
                } else {
                    if users.count == numOfUsers {
                        onSuccess(users)
                    } else {
                        users.append(self.collection.document(userID))
                        print("added")
                        return
                    }
                }
            } else {
                Services.crud.reads(query: self.collection.whereField("quickID", isLessThanOrEqualTo: "\(UInt64.random(in: UInt64.min ... UInt64.max))").limit(to: 1), onSuccess: { (snapshot) in
                    let userID = snapshot.documents.first!.documentID
                    if cUser.userID == userID {
                        Services.crud.reads(query: self.collection.whereField("quickID", isGreaterThanOrEqualTo: "\(UInt64.random(in: UInt64.min ... UInt64.max))").limit(to: 1), onSuccess: { (snapshot) in
                            if users.count == numOfUsers {
                                onSuccess(users)
                            } else {
                                users.append(self.collection.document(userID))
                                print("added")
                                return
                            }
                        }) { (e) in
                            print(e)
                        }
                    } else {
                        if users.count == numOfUsers {
                            onSuccess(users)
                        } else {
                            users.append(self.collection.document(userID))
                            print("added")
                            return
                        }
                    }
                }) { (e) in
                    print(e)
                }
            }
        }) { (e) in
            print(e)
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.