我的firebase数据库中有两个跟踪喜欢和评论通知的节点。每个值都被解析为Notification类,我希望能够从每个节点获取快照并创建合并的通知数组。我有两个函数来提取相应的数据,但我不知道如何合并这两个数组。我试图使用集合,然后形成一个联合,但我不能用非hashable值。我能想到的唯一其他解决方案是将两个功能合二为一,然而,这似乎看起来非常错误。这是我正在使用的......
var commentNotifications = [Notifications]()
func commentNotificationObserver(_ update: @escaping () -> Void) {
commentHandle = CURRENT_USER_COMMENT_NOTIFICATION.observe(DataEventType.value, with: { (snapshot) in
self.commentNotifications.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let id = child.key
let dict = child.value as! Dictionary<String, Any>
let notif: Notifications = Notifications(notifID: id, data: dict)
self.commentNotifications.insert(notif, at: 0)
update()
}
if snapshot.childrenCount == 0 {
update()
}
})
}
// Detects New Likes
var notifications = [Notifications]()
func likeNotificationObserver(_ update: @escaping () -> Void) {
likesHandle = CURRENT_USER_LIKE_NOTIFICATION.observe(DataEventType.value, with: { (snapshot) in
self.notifications.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
for dictionary in child.value as! Dictionary<String, Any> {
let id = dictionary.key
let dict = dictionary.value as! Dictionary<String, Any>
let notif: Notifications = Notifications(notifID: id, data: dict)
self.notifications.insert(notif, at: 0)
update()
}
}
if snapshot.childrenCount == 0 {
update()
}
})
}
因为你可以看到我有一个通知数组和一个 commentNotification 数组。如何在没有重复的情况下将它们放入同一个数组中。谢谢!