Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 513 characters in body
Source Link
Frank van Puffelen
  • 603.3k
  • 85
  • 895
  • 869

Trying to handle an exception as flow control is a bad idea here. See for an explanation why that is: Why not use exceptions as regular flow of control?

So instead of handling the exception that you get when uid is null or empty, check whether it has a value first and then only call the Firestore API with it if it has a value. For example:

if !(uid ?? "").isEmpty { Firestore.firestore().collection("users") .document(uid) .collection("user-convo") ... } 

There is no specific operation to check whether a collection exists.

Instead collection is comes into existence when you add the first document to it, and disappears when you remove the last document from it.

So to check whether a collection exists, the simplest way is to try and read a single document from it.

Firestore.firestore().collection("users") .document("theUID") .collection("user-convo") .limit(to: 1) // 👈 .getDocuments { snapshot, _ in if !snapshot!.isEmpty ... // 👈 } 

Trying to handle an exception as flow control is a bad idea here. See for an explanation why that is: Why not use exceptions as regular flow of control?

So instead of handling the exception that you get when uid is null or empty, check whether it has a value first and then only call the Firestore API with it if it has a value. For example:

if !(uid ?? "").isEmpty { Firestore.firestore().collection("users") .document(uid) .collection("user-convo") ... } 

Trying to handle an exception as flow control is a bad idea here. See for an explanation why that is: Why not use exceptions as regular flow of control?

So instead of handling the exception that you get when uid is null or empty, check whether it has a value first and then only call the Firestore API with it if it has a value. For example:

if !(uid ?? "").isEmpty { Firestore.firestore().collection("users") .document(uid) .collection("user-convo") ... } 

There is no specific operation to check whether a collection exists.

Instead collection is comes into existence when you add the first document to it, and disappears when you remove the last document from it.

So to check whether a collection exists, the simplest way is to try and read a single document from it.

Firestore.firestore().collection("users") .document("theUID") .collection("user-convo") .limit(to: 1) // 👈 .getDocuments { snapshot, _ in if !snapshot!.isEmpty ... // 👈 } 
Source Link
Frank van Puffelen
  • 603.3k
  • 85
  • 895
  • 869

Trying to handle an exception as flow control is a bad idea here. See for an explanation why that is: Why not use exceptions as regular flow of control?

So instead of handling the exception that you get when uid is null or empty, check whether it has a value first and then only call the Firestore API with it if it has a value. For example:

if !(uid ?? "").isEmpty { Firestore.firestore().collection("users") .document(uid) .collection("user-convo") ... } 
Notice added Posted by Recognized Member/Admin in Google Cloud Frank van Puffelen