gpt4 book ai didi

ios - Firebase iOS Swift - 检索与聊天对话 ID 对应的子数据

转载 作者:行者123 更新时间:2023-11-30 13:12:56 25 4
gpt4 key购买 nike

我正在使用 Firebase 构建一个应用,该应用在表格 View 中显示问题列表。当用户点击问题(表格 View 中的单元格)时,应用程序会转到另一个 View Controller ,该 View Controller 显示与该问题关联的答案列表。

这就是我构建数据库 JSON 树的方式:

{
"answers": {
"question01": {
"answer01": {
"name": "kelly"
"text": "I'm doing great"
},
"answer02": {
"name": "george"
"text": "never been better"
}
}
},

"questions": {
"question01": {
"name": "courtney"
"text": "how are you?"
},
"question02": {
"name": "bob"
"text": "why is the earth round?"
}
}

我可以在第一个 TableView 中显示问题,使用以下代码没有问题:

    // MARK: - Firebase Database Configuration
func configureDatabase() {//this method gets called in viewDidLoad

ref = FIRDatabase.database().reference()

//listen for new questions in the database
_refHandle = self.ref.child("questions").observeEventType(.ChildAdded, withBlock: {(snapshot) -> Void in

self.questionsArray.append(snapshot)
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.questionsArray.count-1, inSection: 0)], withRowAnimation: .Automatic)
})
}

deinit {

self.ref.child("questions").removeObserverWithHandle(_refHandle)
}


// MARK: - UITableViewDataSource & UITableViewDelegate methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return questionsArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: UITableViewCell! = self.tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPath)

//unpack question from database
let questionSnapshot: FIRDataSnapshot! = self.questionsArray[indexPath.row]
let question = questionSnapshot.value as! Dictionary<String, String>
let name = question[Constants.QuestionFields.name] as String!
let text = question[Constants.QuestionFields.text] as String!

cell!.textLabel?.text = name + ": " + text
cell!.imageView?.image = UIImage(named: "ic_account_circle")
if let photoUrl = question[Constants.QuestionFields.photoUrl], url = NSURL(string:photoUrl), data = NSData(contentsOfURL: url) {
cell!.imageView?.image = UIImage(data: data)
}

return cell!
}

已关注 Firebase's guide to structuring data ,我假设我需要使用 JSON 树中每个子项共有的对话 ID 来检索与问题关联的答案。例如:“question01”将是第一个有两个答案的问题的对话 ID。

如何检索与每个问题关联的答案数据,以便稍后可以在表格 View 中显示这些答案?

需要明确的是,我不是问如何在 TableView 中显示数据,而是问从与问题对话 ID 关联的 Firebase 数据库检索答案数据的代码是什么。

最佳答案

有多种方法可以回答这个问题。

假设我们在第一个 View Controller 中有一个问题表格 View ,并且数据源包含对问题的引用:question01、question02 等。

根据 Firebase 结构,当用户点击第一个问题 (question01) 时,节点名称将传递到第二个 View Controller 。它可以分配给 Controller 内的 var 或通过 segue 传递,甚至可以使用表示的对象进行设置。

无论如何,第二个 View Controller 都会知道问题节点是什么(例如 Question01),然后从 viewDidLoad 加载来自 Firebase 的数据并更新 tableView 就很简单了。

var questionRef: String() //this will contain question01 for example

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadFromFirebase()
}

然后

func loadFromFirebase {

let ref = answersRef.child(questionRef) //will be answers/question01
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

for child in snapshot.children {

let key = child.key as String
let text = child.value["text"] as! String
print("answer: \(key) text: \(text)")
//add data to a tableViewDataSource
}
//reload the tableview
})
}

其输出是

answer: answer01  text: I'm doing great
answer: answer02 text: never been better

关于ios - Firebase iOS Swift - 检索与聊天对话 ID 对应的子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38537028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com