когда я удаляю книгу из представления коллекции, она удаляет ее из пользовательского интерфейса, но удаление не отражается в моей базе данных firestore. Я пробовал .delete (), но он удаляет весь массив книг. Я хочу удалить только книгу, выбранную при просмотре коллекции. В настоящее время я пытаюсь передать свой массив книг let userBooks: [UserBooks] = []
в качестве параметра.
введите описание изображения здесь
fileprivate func removeUserBooksTest(userBooks: UserBooks) {
guard let uid = Auth.auth().currentUser?.uid else { return }
Firestore.firestore().collection("books").document(uid).updateData(["books": FieldValue.arrayRemove([userBooks])]) { (error) in
if let error = error {
print(error)
return
}
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Create a option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//Add the cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
//Add the remove book button
let removeBook = UIAlertAction(title: "Remove Book", style: .destructive) { (action: UIAlertAction) in
self.removeUserBooksTest(userBooks: self.userBooks[indexPath.item])
self.userBooks.remove(at: indexPath.item)
self.collectionView.deleteItems(at: [indexPath])
}
optionMenu.addAction(removeBook)
//Display the menu
present(optionMenu, animated: true)
}
}