在 Firebase 中更改值时更新数组 [[String: [Dictionary]]]
Update Array [[String: [Dictionary]]] when valueChanged in Firebase
var chatMessages = [[String: ChatMessage]]()
Firebase 聊天消息结构是这样的。
-Kjws99ol6qjFt7ET9C
content: "Hehd"
displayName: "John Doe"
fileLength: 0
fileUrl: ""
fromID: "5904ee8cfa"
isRead: false
messageStatus: 2
messageType: "normal"
timestamp: 1494596232
现在 childAdded
我将这样附加新消息
weakSelf.chatMessages.append(newMessage)
//Kjws99ol6qjFt7ET9C - 这是存储在 String 中的线程 ID,下面的值存储在 ChatMessage
中
但是在消息被用户读取后它的值发生变化并且由 childChanged
标识因此在 childChanged 中更改如何正确更新我的数组?
我不明白为什么要将字典存储在数组中。在我看来像
var chatMessages = [String: ChatMessage]()
会满足您的模型,因为每个消息推送 ID(例如 -Kjws99ol6qjFt7ET9C
)在数据库中都是唯一的,因此您可以通过将此推送 ID 存储为键来获得有效的字典。
当您收到 childChanged
事件时,您可以通过在字典中查找 snapshot.key
并更新它来快速定位已更改的消息。
在 childChanged 事件中,应用程序收到更新后的子节点的快照,(在本例中)键为 Kjws99ol6qjFt7ET9C,值为子节点数据。
要更新数组,找到 chatMessages 数组中的哪个索引对应于该键并相应地更新值。
要在您设置的数组中找到它,这是一个 [String: ChatMessage] 字典数组,请执行以下操作
let searchKey = "Kjws99ol6qjFt7ET9C"
let index = chatMessages.map( {[=10=].keys.first!} ).index(of: searchKey)
获得索引后,您可以使用新数据更新数组中的元素。
{[=11=].keys.first!} - compiles all of the keys in the chatMessage array into an array
index(of: searchKey) - finds the index of the searchKey we are looking for
那你可以
chatMessage[index] = updated data
如果您需要任何其他代码,请告诉我。
但是,我强烈建议更改模型以在数组中存储 ChatMessage class(或结构)
class ChatMessage {
var fbKey = "" // the key like Kjws99ol6qjFt7ET9C
var content = "" // like "Hehd"
var displayName "" // "John Doe"
}
var chatMessages = [ChatMessage]()
维护起来会更容易,数组搜索会更简单更快。
对于此用例,要查找特定索引,请执行此操作
let searchKey = "Kjws99ol6qjFt7ET9C"
let index = chatMessages.index(where: { [=14=].fbKey == searchKey} )
var chatMessages = [[String: ChatMessage]]()
Firebase 聊天消息结构是这样的。
-Kjws99ol6qjFt7ET9C
content: "Hehd"
displayName: "John Doe"
fileLength: 0
fileUrl: ""
fromID: "5904ee8cfa"
isRead: false
messageStatus: 2
messageType: "normal"
timestamp: 1494596232
现在 childAdded
我将这样附加新消息
weakSelf.chatMessages.append(newMessage)
//Kjws99ol6qjFt7ET9C - 这是存储在 String 中的线程 ID,下面的值存储在 ChatMessage
但是在消息被用户读取后它的值发生变化并且由 childChanged
标识因此在 childChanged 中更改如何正确更新我的数组?
我不明白为什么要将字典存储在数组中。在我看来像
var chatMessages = [String: ChatMessage]()
会满足您的模型,因为每个消息推送 ID(例如 -Kjws99ol6qjFt7ET9C
)在数据库中都是唯一的,因此您可以通过将此推送 ID 存储为键来获得有效的字典。
当您收到 childChanged
事件时,您可以通过在字典中查找 snapshot.key
并更新它来快速定位已更改的消息。
在 childChanged 事件中,应用程序收到更新后的子节点的快照,(在本例中)键为 Kjws99ol6qjFt7ET9C,值为子节点数据。
要更新数组,找到 chatMessages 数组中的哪个索引对应于该键并相应地更新值。
要在您设置的数组中找到它,这是一个 [String: ChatMessage] 字典数组,请执行以下操作
let searchKey = "Kjws99ol6qjFt7ET9C"
let index = chatMessages.map( {[=10=].keys.first!} ).index(of: searchKey)
获得索引后,您可以使用新数据更新数组中的元素。
{[=11=].keys.first!} - compiles all of the keys in the chatMessage array into an array
index(of: searchKey) - finds the index of the searchKey we are looking for
那你可以
chatMessage[index] = updated data
如果您需要任何其他代码,请告诉我。
但是,我强烈建议更改模型以在数组中存储 ChatMessage class(或结构)
class ChatMessage {
var fbKey = "" // the key like Kjws99ol6qjFt7ET9C
var content = "" // like "Hehd"
var displayName "" // "John Doe"
}
var chatMessages = [ChatMessage]()
维护起来会更容易,数组搜索会更简单更快。
对于此用例,要查找特定索引,请执行此操作
let searchKey = "Kjws99ol6qjFt7ET9C"
let index = chatMessages.index(where: { [=14=].fbKey == searchKey} )