无法从Firebase数据库和存储中删除数据
我正在使用以下代码将照片和数据上传到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()
这段代码没有任何问题,必须工作正常,那么问题出在哪里?
试试下面的代码:
var photoRef = DatabaseReference()
放在viewDidLoad中:
photoRef = Database.database().reference().child("posts")
您必须存储在The_Unique_id somewhere.And你将有图像的URL你想删除。请点击:statusRef.child("The_Unique_id").child("Pass_Your_imageUrl_Here").removeValue()
。
我尝试了您的代码,但无法正常工作 –
试试这个
Api.MyPosts.REF_MYPOSTS.child(currentUserId).removeValue()
Api.MyPosts.REF_MYPOSTS.child(newPostId).removeValue()
此代码无法使用! –
您可以通过调用'在'FIRDatabaseReference' removeValue'到数据删除数据。请参阅https://firebase.google.com/docs/database/ios/read-and-write#delete_data –