【发布时间】:2020-10-23 07:58:21
【问题描述】:
我有一个名为 objects 的集合/Object 类型的文档,它有一个 ObjectNotification 类型的数组,我正在尝试将每个 notification.read 更新为 true。
我有一个视图,它显示单个数组 self.viewModel.userNotifications 中所有对象的所有 userNotifications。
当onAppear 用于视图时,我尝试将self.viewModel.userNotifications 中的每个userNotification.read 设置为true 并更新FirestoreDB。
但是我不确定采用的最佳方法,目前我正在遍历数组并尝试更新 self.viewModel.objects.userNotifications 中的每个 userNotification,然后更新数据库中的文档,这将更新 self.viewModel.userNotifications,因为它会获取所有 @ 987654335@.
但是当我尝试更改结构时出现以下错误,我试图更改 for in 语句中的值,然后调用我的 updateObect(object) 方法来更新数据库中的文档。
不能通过下标赋值:'h' 是一个 'let' 常量
.onAppear() {
// Mark as read
let read = objectManager.markNotificationsAsRead(self.viewModel.userNotifications)
self.viewModel.updateObjectNotifications(readNotifcations: read)
}
func markNotificationsAsRead(_ notifications: [ObjectNotification]) -> [ObjectNotification]{
// Mark notifications as read
var readNotifications: [ObjectNotification] = []
for n in notifications {
if n.read == false {
// Create new with true
var new = n
new.read = true
readNotifications.append(new)
}
}
// Return update notifications with read = true
return readNotifications
}
func updateObjectNotifications(readNotifcations: [ObjectNotification]) {
if let currentUser = Auth.auth().currentUser {
for h in self.objects {
for n in h.notifications {
if n.deliveredTo == currentUser.email {
for r in readNotifcations {
if r.id == n.id {
// same notif for home
// h.notifications.rem
if let index = h.notifications.firstIndex(of: n) {
h.notifications[index] = r // Cannot assign through subscript: 'h' is a 'let' constant
}
}
}
}
// update object in db
}
}
}
}
除了上面的方法,如何更改数据库中的字段?
【问题讨论】:
-
请添加更多代码以使其成为minimal, reproducible example。
-
这个问题非常模糊和不完整。例如。你问的是
for h in self.objects {这一行,但我们不知道self.objects是什么。我们也不知道您的任何 Classes 或 Structs 是什么样的 - 但是,如果您的h对象(无论是什么)的属性.notifications是 let 属性,它就无法更改。您能否阐明您实际尝试做什么(使用 Firebase)并添加有关您的模型外观的更多信息?
标签: swift firebase google-cloud-firestore