我正在使用下面的代码将照片和数据上传到 firebase。
static func sendDataToDatabase(photoUrl: String, videoUrl: String? = nil, ratio: CGFloat, caption: String, Location: String, onSuccess: @escaping () -> Void) {
let newPostId = Api.Post.REF_POSTS.childByAutoId().key
let newPostReference = Api.Post.REF_POSTS.child(newPostId)
guard let currentUser = Api.User.CURRENT_USER else {
return
}
let currentUserId = currentUser.uid
let words = caption.components(separatedBy: CharacterSet.whitespacesAndNewlines)
for var word in words {
if word.hasPrefix("#") {
word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)
let newHashTagRef = Api.HashTag.REF_HASHTAG.child(word.lowercased())
newHashTagRef.updateChildValues([newPostId: true])
}
}
let timestamp = Int(Date().timeIntervalSince1970)
var dict = ["uid": currentUserId ,"photoUrl": photoUrl, "caption": caption, "Location": Location, "likeCount": 0, "ratio": ratio, "timestamp": timestamp] as [String : Any]
if let videoUrl = videoUrl {
dict["videoUrl"] = videoUrl
}
newPostReference.setValue(dict, withCompletionBlock: {
(error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
Api.Feed.REF_FEED.child(Api.User.CURRENT_USER!.uid).child(newPostId)
.setValue(["timestamp": timestamp])
Api.Follow.REF_FOLLOWERS.child(Api.User.CURRENT_USER!.uid).observeSingleEvent(of: .value, with: {
snapshot in
let arraySnapshot = snapshot.children.allObjects as! [DataSnapshot]
arraySnapshot.forEach({ (child) in
print(child.key)
Api.Feed.REF_FEED.child(child.key).child(newPostId)
.setValue(["timestamp": timestamp])
let newNotificationId = Api.Notification.REF_NOTIFICATION.child(child.key).childByAutoId().key
let newNotificationReference = Api.Notification.REF_NOTIFICATION.child(child.key).child(newNotificationId)
newNotificationReference.setValue(["from": Api.User.CURRENT_USER!.uid, "type": "feed", "objectId": newPostId, "timestamp": timestamp])
})
})
let myPostRef = Api.MyPosts.REF_MYPOSTS.child(currentUserId).child(newPostId)
myPostRef.setValue(["timestamp": timestamp], withCompletionBlock: { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
})
ProgressHUD.showSuccess("Success")
onSuccess()
})
}
我使用一个按钮和下面的代码来删除照片并从 Firebase 中删除数据。
@IBAction func DeletePost(_ sender: Any) {
guard let currentUser = Api.User.CURRENT_USER else {
return
}
let newPostId = Api.Post.REF_POSTS.childByAutoId().key
let currentUserId = currentUser.uid
let newPostReference = Api.Post.REF_POSTS.child(newPostId)
let alert = UIAlertController(title: "Delete Post", message: "Are You Sure ?", preferredStyle: .alert);
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil));
let okAction = UIAlertAction(title: "Delete", style: .default) {(action) in
Api.MyPosts.REF_MYPOSTS.child(currentUserId).child(newPostId).removeValue()
{ (error, ref) in
if error != nil {
print("error")
return
}
print("Posts Deleted")
}
};
alert.addAction(okAction);
self.present(alert, animated: true, completion: nil);
}
该项目运行良好,但是当我触摸按钮删除帖子时,没有任何反应!
Api.MyPosts.REF_MYPOSTS.child(currentUserId).child(newPostId).removeValue()
这段代码没有任何问题,必须正常工作,那么问题出在哪里?