Swift Firestore Check If Documents Exists

Swift - How do I check if a firestore document exists?

@JRLtechwriting solved my problem.

"Use if let document = document, document.exists {. Check out the example here: https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document"

Firestore Swift 5: Validating if document username data exists

Your code:

  1. Loads all user documents
  2. Loops over these documents
  3. Writes a new document for this user if it finds any document with a different name

So as soon as there's any user who has a different name than the one you're trying to add, you add a document for the user. It seems your use-case is to only add a new document if none of the documents match the name of the new user.

There are two ways to do that:

  1. Use a boolean variable to check if you're found a matching name, setting it to true in the loop if you find a match. Then after the loop, only add the new document if the variable is still false.

  2. Use a query to only load documents with the username that the new user entered.

While #1 is closest to your existing code, it requires that you load all user documents. That would get slower and slower as your app gets more users, so is not recommending. I'd instead go with approach #2 and use a query.

That'd look something like this:

db.collection("Users")
.whereField("username", isEqualTo: username)
.getDocuments { (querySnapshot, error) in
if let error = error {
print("Error connecting to database")
}
else {
if querySnapshot.isEmpty {

let userID = Auth.auth().currentUser!.uid

if let name = self.nameField.text, let email = self.emailTextField.text, let username = self.usernameTextField.text {
self.db.collection("Users").document("\(userID)").setData(["name":name,"email":email, "username":username, "accountBalance": 0]) { (error) in

if error != nil {
print("Error saving data")
}
else {
self.performSegue(withIdentifier: "signedUp", sender: self)
}
}
}
}
}
}

Also see:

  • swift firestore check if documents exists (which shows how to create a separate collection with "claimed user names").

How can I tell if I value already exists in Firebase Firestore?

There isn't any function to check if a field exists in a document. You'll have to fetch the document and check for it's existence:

let docRef = Firestore.firestore().collection(user!.uid).document(docID)

docRef.getDocument { (document, error) in
if let document = document, document.exists {
let data = document.data()
// Check if the field exists in data
} else {
print("Document does not exist")
}
}

Swift & Firestore - check for document in collection always returns false

Is there something about this closure that it is returning the value before it finishes checking the server?

Yes, the closure is executed asynchronously, not immediately. getDocument does not block the code, and it returns immediately, before the query is complete. The callback is invoked some time later with the results. This is to avoid making your code block the UI thread, which would cause your app to become unresponsive during the query. Read more about why the Firebase APIs are asynchronous.

The function, as you have it written now, will always return false, because the initial value of doesCodeExist will not change by the time its value is returned.



Related Topics



Leave a reply



Submit