У меня проблемы с отправкой поста в комментарии vc, чтобы я мог сохранить его в Firebase.
Я следую онлайн-уроку, где мы создаем эту архитектуру на Firebase, однако, когдаЯ отправляю данные в Firebase, а не postId, как видно на на этом скриншоте .Я явно что-то упускаю.Был бы очень признателен, если бы кто-то мог пролить свет на то, что моя ошибка может быть.Ниже я добавлю функцию «Контроллер представления комментариев», куда я отправляю данные в Firebase.
@IBAction func sendButtonPressed(_ sender: UIButton) {
let ref = Database.database().reference()
let commentsReference = ref.child("comments")
let newCommentId = commentsReference.childByAutoId().key
let newCommentReference = commentsReference.child(newCommentId)
//current user information
guard let currentUser = Auth.auth().currentUser else {
return
}
let currentUserId = currentUser.uid
newCommentReference.setValue(["userid": currentUserId, "commentText": commentTextField.text!]) { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
let postCommentRef = Database.database().reference().child("post-comments").child(self.postIdNew).child(newCommentId)
postCommentRef.setValue(true, withCompletionBlock: { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
})
self.empty()
self.view.endEditing(true)
}
}
Так я должен получить ссылку на postId из контроллера Home View.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "commentSegue" {
let commentVC = segue.destination as! CommentViewController
let postId = sender as! String
commentVC.postIdNew = postId
}
}
Вот мои расширения представления коллекции
extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.lostCollectionView {
return posts.count
}
if collectionView == self.foundCollectionView {
return newPostsFound.count
}
if collectionView == self.adoptionCollectionView {
return postsadoption.count
}
else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView {
case lostCollectionView:
let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell
let post = posts[indexPath.row]
let user = users[indexPath.row]
lostcell.post = post
lostcell.user = user
//Make TextView Clickable
lostcell.phoneLostTextView.isEditable = false;
lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
//Make Comments View Clickable
lostcell.homeVC = self
return lostcell
case foundCollectionView:
let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell
let post = newPostsFound[indexPath.row]
foundcell.post = post
//Make TextView Clickable
foundcell.phoneFoundTextView.isEditable = false;
foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return foundcell
case adoptionCollectionView:
let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell
let post = postsadoption[indexPath.row]
adoptioncell.post = post
//Make TextView Clickable
adoptioncell.phoneAdoptionTextView.isEditable = false;
adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return adoptioncell
default:
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch collectionView {
case lostCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "lostSelectedViewController") as? LostSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = posts[indexPath.row]
break
case foundCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "foundSelectedViewController") as? FoundSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = newPostsFound[indexPath.row]
break
case adoptionCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "adoptionSelectedViewController") as? AdoptionSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = postsadoption[indexPath.row]
break
default:
break
}
func commentsViewPressed() {
print("Hola")
performSegue(withIdentifier: "commentSegue", sender: self)
}
}
}