我有下面的Firebase查询用于解析到我的tableView
{
"-LC8fVE90ovEFRQAhhdv" : {
"description" : "Bu grupta arkadaşlar takılsınlar",
"members" : [ "VSVmIF4o0ie2QMIcXdTDTcC8Hq93", "hJgM5tYU45VmjaMTzYAG4Yo3RyD2", "YrLEyhWf9KOIwoK2YN7HrzxpwMc2" ],
"title" : "Takılmaca"
},
"-LC8wUR-11bX5FnjQ6Mk" : {
"description" : "Hello",
"members" : [ "VSVmIF4o0ie2QMIcXdTDTcC8Hq93", "hJgM5tYU45VmjaMTzYAG4Yo3RyD2", "nkwPkD2FeDZZFDnLunTe3D2EuQr2", "YrLEyhWf9KOIwoK2YN7HrzxpwMc2" ],
"messages" : {
"-LC9Pj9krCB4kZnXtXc8" : {
"content" : "Its me",
"senderId" : "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"
},"-LC9Pj9krCB4kasfXtXc8" : {
"content" : "Who are you",
"senderId" : "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"
}
}
我试图用以下代码获取内容和senderId
var groupMessages = [Message]()
REF_GROUPS.observeSingleEvent(of: .value) { (groups) in
guard let groups = groups.children.allObjects as? [DataSnapshot] else { return }
for group in groups {
let groupss = group.value as? NSDictionary
if let messages = groupss!["messages"] as? [String: Any] {
}
}
handler(groupMessages)
}
但是,由于messages对象是由随机标识符(如LC9Pj9krCB4kZnXtXc8)创建的,所以我无法访问内容。如何获取该内容和senderId?
最佳答案
第一件事是不要在NSDictionary
中使用NSArray
或Swift
,这会使事情变得复杂。其次,只需检查snapshot.exists()
,而不是为guard
设置allObjects
。for group in groups
应该替换为for child in snapshot.children
,child
本身就是一个DataSnapshot
。见以下代码:
REF_GROUPS.observeSingleEvent(of: DataEventType.value) { (snapshot) in
if snapshot.exists() {
var allMessages: Array<Dictionary<String, String>> = []
for child in snapshot.children {
let childSnapshot = child as! DataSnapshot
guard let dictValue = childSnapshot.value as? Dictionary<String, Any>, let messages = dictValue["messages"] as? Dictionary<String, Any> else {continue}
if let array = Array(messages.values) as? Array<Dictionary<String, String>> {
allMessages.append(contentsOf: array)
}
}
/// All messages into single array
print(allMessages)
/// All senderIds
let senderIds = allMessages.map({$0["senderId"]!})
print(senderIds)
/// Unique senderIds
let uniqueSenderIds = Array(Set(senderIds))
print(uniqueSenderIds)
/// Grouping the senderId with messages
var finalDict: Dictionary<String, [String]> = [:]
for data in allMessages {
guard let senderId = data["senderId"], let content = data["content"] else {continue}
if var oldValue = finalDict[senderId] {
oldValue.append(content)
}
else {
finalDict[senderId] = [content]
}
}
print(finalDict)
/// Inbuilt Dictionary grouping function
let dictionary = Dictionary(grouping: allMessages, by: {$0["senderId"]!})
print(dictionary)
}
输出:所有消息
[["content": "Its me", "senderId": "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"],["content": "Who are you", "senderId": "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"]]
输出:senderID
["YrLEyhWf9KOIwoK2YN7HrzxpwMc2", "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"]
输出:uniqueSenderIds
["YrLEyhWf9KOIwoK2YN7HrzxpwMc2"]
输出:finalDict
["YrLEyhWf9KOIwoK2YN7HrzxpwMc2": ["Its me", "Who are you"]]
输出:finalDict
["YrLEyhWf9KOIwoK2YN7HrzxpwMc2": [["content": "Its me", "senderId": "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"], ["content": "Who are you", "senderId": "YrLEyhWf9KOIwoK2YN7HrzxpwMc2"]]]
我不知道你为什么要获取所有组的内容,但我建议通过
Groups -> GroupId -> messages
ref获取数据,并为此ref放置一个侦听器,以便应用程序可以在收到新消息时得到通知。