正如你看到的,你Link
是.child()
參數"user"
的總結和的標識用戶/消息,所以:
你需要做的是,將autoID
保存到你的消息字典中,以便你可以參考那個和call the message directly
。通過這種方式,您可以「.child()
」獲得您需要的信息,並「間接」創建direct link
而不是查詢整個Firebase。
爲了這個目的,你需要保存的消息像:
//for my example I've created a struct: Message
struct Message {
var message: String = ""
var user: String = ""
var messageID: String = ""
}
func saveMessages(userID: String, message: Message) { // or your dictionary
// here you set an auto id
let reference = firebase.child(userID).childByAutoId()
// here you save the id into your messages dict/model
message.messageID = reference.key
// here you save the dict/model into your firebase
reference.setValue(message) { (error, ref) -> Void in
if error != nil {
print("\(error)")
}
}
}
您可以通過saveMessages(userID: myUserID, messages: message)
然後調用函數您:
加載所有的郵件到數組的結構:
// we create and instantiate an Array of Message
var messages: [Message] = []
func loadAllMessages(userID: String) {
//we query all messages from the certain user
let usersRef = firebase.child(userID).child("messages")
usersRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
// since we use observeEventType we need to clear our Array
// everytime our snapshot exists so we're not downloading
// single messages multiple times
self.messages.removeAll()
// I'm always sorting for date
// even if your dict has no date, it doesnt crash
let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "date",ascending: false)])
// now we loop through sorted to get every single message
for element in sorted {
let message = element.valueForKey("message")! as? String
let name = element.valueForKey("name")! as? String
let messageID = element.valueForKey("messageID")! as? String
// we're creating a message model
let m = Message(message: message!, name: name!, messageID: messageID!)
// and saving it into our array
self.messages.append(m)
}
}
})
}
或者你直接打電話您(需要)已經知道的ID的消息。
func loadSingleMessages(userID: String, messageID: String) {
// we use the direct "link" to our message
let usersRef = firebase.child(userID).child(messageID)
usersRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
let message = snapshot.valueForKey("message")! as? String
let name = snapshot.valueForKey("name")! as? String
let messageID = snapshot.valueForKey("messageID")! as? String
// create the model
let m = Message(message: message!, name: name!, messageID: messageID!)
// and save it to our Array
self.messages.append(m)
}
})
}
摘要:要能直接打電話給你的信息,而不必通過整個火力地堡查詢,並有循環用自己的方式所需的信息,你需要知道產生autoID
,存儲和使用該ID查詢參考。
例如,您可以在用戶的Firebase中設置一個數組,以存儲messageID。然後你可以使用它們來查詢你想要的信息。
事情是這樣的:
struct User {
var userID: String = ""
var name: String = ""
var email: String = ""
var profileImageURL = ""
var messages: [String] = []
}
let user = User()
你然後下載並從火力地堡實例化您的用戶模型後,您:
// for every message in our user.messages we call our function
for message in user.messages {
loadSingleMessages(userID: user.userID, messageID: message)
}
順便說一句,郵件ID也很重要,要能刪除或編輯某個消息。
非常感謝你這個詳細的答案。我今天會嘗試這個,並向你報告! –
不客氣的朋友。快樂的編碼 –