在我的应用中,当用户取消关注另一个用户时,我希望从firebase中删除相应的关注通知。但是,当我更新该通知的引用时,观察功能将立即触发,新通知将被删除。
那么,如果有一种方法,我如何在不触发观察者功能的情况下更新通知值?
以下是我上传通知并删除“关注通知”的功能
func uploadNotification(toUser user: User, type: NotificationType, postID: String? = nil){
guard let uid = Auth.auth().currentUser?.uid else { return }
//users do not get notifications of themselevs
guard uid != user.uid else { return }
var values: [String: Any] = ["timestamp": Int(NSDate().timeIntervalSince1970),
"uid": uid,
"type": type.rawValue]
if let postID = postID {
values["postID"] = postID
}
REF_NOTIFICATIONS.child(user.uid).childByAutoId().updateChildValues(values) { (error, ref) in
guard let reference = ref.key else { return }
if type == .follow {
REF_FOLLOW_NOTIFICATIONS.child(user.uid).updateChildValues([reference: String(type.rawValue)])
}
}
}
func unfollowUser(uid: String, completion: @escaping DatabaseCompletion) {
guard let currentUID = Auth.auth().currentUser?.uid else { return }
self.deleteFollowNotification(uid: uid)
REF_USER_FOLLOWING.child(currentUID).child(uid).removeValue { (error, ref) in
REF_USER_FOLLOWERS.child(uid).child(currentUID).removeValue(completionBlock: completion)
}
}
func deleteFollowNotification(uid: String){
REF_FOLLOW_NOTIFICATIONS.child(uid).observeSingleEvent(of: .value) { snapshot in
let notificationKey = snapshot.key
REF_NOTIFICATIONS.child(uid).child(notificationKey).removeValue()
}
REF_FOLLOW_NOTIFICATIONS.child(uid).removeValue()
}
答案 0 :(得分:1)
不可能使数据库观察者完全忽略特定的更新。您将不得不完全删除观察者,或者在其中编写一些代码来决定在调用它时是否应该采取措施。