I have a function to fetch convos and it worked but then I deleted all the documents in Firebase. So now when I run it, it says "document path cannot be empty" and the app crashes. I am not very familiar with Swift but in Python I simply just use a try and except.
In the try block I can simply copy and paste all my code, and the except block I just do my error handling. I'm not sure how to do this in Swift for my entire function. How can I rearrange my function so that the function body is inside a do/try block?
Also what is the most strategic spot to do my error handling, the viewModel file or the Service file? The viewModel file inherits functions from the service file.
viewModel file:
func fetchConvos(){
var userList: [User] = []
service.getConversations() { users in
userList = users
userList.forEach { user in
var messList: [Message] = []
}
}
}
service file:
func getConversations(completion: @escaping([User]) -> Void){
Firestore.firestore().collection("users")
.document(uid)
.collection("user-convo")
.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
documents.forEach { doc in
let userID = doc.documentID
users.append(userID)
completion(users)
}
}
}