我正在使用以下代码将照片和数据上传到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()
此代码没有任何问题,必须正常工作,问题出在哪里?
答案 0 :(得分:1)
请尝试以下代码:
var photoRef = DatabaseReference()
放入ViewDidLoad:
photoRef = Database.database().reference().child("posts")
您必须已将The_Unique_id存储在某处。您将获得要删除的图像的网址。
点击按钮:statusRef.child("The_Unique_id").child("Pass_Your_imageUrl_Here").removeValue()
。
答案 1 :(得分:0)
试试这个
syso.call("print", new Object[]{"I", "am", "in", "syso"});