我正在使用以下代碼將照片和數據上傳到Firebase。無法從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()
})
}
使用按鈕和下面的代碼刪除照片並從火力地堡
@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()
這段代碼沒有任何問題,必須工作正常,那麼問題出在哪裏?
您可以通過調用'在'FIRDatabaseReference' removeValue'到數據刪除數據。請參閱https://firebase.google.com/docs/database/ios/read-and-write#delete_data –