Ich verwende den folgenden Code zum Hochladen von Fotos und Daten zu Firebase.Die Daten aus der Firebase-Datenbank und dem Speicher können nicht entfernt werden.
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()
})
}
Verwendung Taste und unter Code zum Löschen Foto und nehmen Sie Daten von 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);
}
Projekt gut laufen, aber wenn Touch-Taste zum Löschen Post nichts passiert!
Api.MyPosts.REF_MYPOSTS.child (CurrentUserID) .child (newPostId) .removeValue()
Dieser Code haben kein Problem und muss arbeiten gut so, wo ist das Problem?
Sie Daten löschen, indem Sie 'removeValue' auf einem' FIRDatabaseReference' auf diese Daten aufrufen. Siehe https://firebase.google.com/docs/database/ios/read-and-write#delete_data –