【发布时间】:2016-08-10 20:00:45
【问题描述】:
我正在使用 Firebase 构建一个在表格视图中显示问题列表的应用。每个表格视图单元格都包含问题的文本、发布问题的用户的姓名以及发布问题的用户的个人资料照片。
这是我构建 JSON 树的方式:
"questions"
"firebase_autoID_01"
"name": "Jim"
"text": "Why is the earth round?"
"userID": "123456"
"userInfo"
"userID": "123456"
"photoURL": "gs://default_photo"
我正在尝试检索每个问题的 name 和 text 以及具有相应 userID 的用户的 photoURL,然后将这 3 个元素放在每个表格视图单元格中。所以我试图从questions 子节点和单独的userInfo 子节点中检索数据,以将该数据放入同一个表格视图单元格中。这是我一直在尝试的代码:
当第一次创建用户时,我将他们的 photoURL 设置为我手动上传到 Firebase 存储的默认图像:
...
let placeholderPhotoRef = storageRef.child("Profile_avatar_placeholder_large.png")
let placeholderPhotoRefString = "gs://babble-8b668.appspot.com/" + placeholderPhotoRef.fullPath
//let placeholderPhotoRefURL = NSURL(string: placeholderPhotoRefString)
let data = [Constants.UserInfoFields.photoUrl: placeholderPhotoRefString]
self.createUserInfo(data)
}
func createUserInfo(data: [String: String]) {
configureDatabase()
let userInfoData = data
if let currentUserUID = FIRAuth.auth()?.currentUser?.uid{
self.ref.child("userInfo").child(currentUserUID).setValue(userInfoData)
}
}
当我尝试从 questions 和 userInfo 检索数据时,问题开始出现:
var photoUrlArray = [String]()
func configureDatabase() {
ref = FIRDatabase.database().reference()
_refHandle = self.ref.child("questions").observeEventType(.ChildAdded, withBlock: {(snapshot) -> Void in
self.questionsArray.append(snapshot)
//unpack the userID from the "questions" child node to indicate which user to get the photoURL from in the "userInfo" child node
if let uid = snapshot.value?[Constants.QuestionFields.userUID] as? String {
self._photoURLrefHandle = self.ref.child("userInfo").child(uid).observeEventType(.ChildAdded, withBlock: {(snapshot) -> Void in
if let photoURL = snapshot.value as? String {
self.photoUrlArray.append(photoURL)
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.questionsArray.count-1, inSection: 0)], withRowAnimation: .Automatic)
}
})
}
})
}
我在 Xcode 控制台中收到以下错误:
“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(2)必须等于更新前该节中包含的行数 (0),加上或减去从该节插入或删除的行数(1 插入,0 删除),加上或减去移入或移出该节的行数(0 移入,0 移出)。'"
我在这里所做的是创建一个 firebase 句柄来观察/检索来自 userInfo 子级的用户 photoURL,并且该句柄是在观察/检索来自 @ 的数据的句柄的闭包内创建的987654335@孩子。
问题:如果这不是检索我想要的数据的正确方法,我应该如何处理?如果 JSON 数据目前的结构不正确,我应该如何构建它?
这是我从表格视图中的questions 子节点解压缩name 和text 的方法,它工作正常:
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]
var 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
return cell!
}
问题:我应该在这里解压photoURL还是在之前的configureDatabase()函数中解压?
【问题讨论】:
标签: ios swift uitableview firebase firebase-realtime-database