【发布时间】:2016-09-26 14:19:51
【问题描述】:
不久前我在 Udemy 上注册了一门课程,内容是关于如何使用 Swift 和 Firebase 创建聊天应用程序,只是因为我想深入了解它是如何工作的。
但是,我在一周后完成了课程。这是几个月前的事了,从那时起 - Firebase 和 Swift 都有更新,我目前正在努力将一些旧代码转换为新代码。
这是我目前正在努力解决的代码:
func loadRecents() {
firebase.child("Recent").queryOrdered(byChild: "userId").queryEqual(toValue: FIRAuth.auth()!.currentUser?.uid).observe(.value) { (snapshot:FIRDataSnapshot) in
self.recents.removeAll()
if snapshot.exists() {
if let values = snapshot.value as? [String:AnyObject] {
let sorted = (values as! NSArray).sortedArray(using: [NSSortDescriptor(key: "date", ascending: false)])
for recent in sorted {
self.recents.append(recent as! NSDictionary)
self.conversationTableView.reloadData()
}
}
self.conversationTableView.reloadData()
}
self.conversationTableView.reloadData()
}
self.checkUnread()
}
所以这只是在 tableView 中显示所有“聊天”的代码,我只是通过 Date 对它进行排序,其中升序为 false。
我所做的更改只是将旧的snapshot.value.allValues 转换为snapshot.value as? [String:AnyObject]。查询按登录账号(FIRAuth.auth().currentUser!.uid)排序。
但是,我很确定此代码已被弃用,因为每次构建此代码时都会出错。而且我知道它在以前的版本中有效。
这是错误:
Thread 1: EXC_BAD_INSTRUCTION
如果有必要,并且碰巧我完全搞砸了当前代码(可能是这样),这里是完整的旧代码:
func loadRecents() {
firebase.child("Recent").queryOrderedByChild("userId").queryEqualToValue(FIRAuth.auth()!.currentUser!.uid).observeEventType(.Value, withBlock: {
snapshot in
self.recents.removeAll()
if snapshot.exists() {
let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "date", ascending: false)])
for recent in sorted {
self.recents.append(recent as! NSDictionary)
//add functio to have offline access as well, this will download with user recent as well so that we will not create it again
firebase.child("Recent").queryOrderedByChild("chatRoomID").queryEqualToValue(recent["chatRoomID"]).observeEventType(.Value, withBlock: {
snapshot in
})
}
}
self.tableView.reloadData()
})
self.checkUnread()
}
关于如何绕过/修复此问题的任何想法?非常感谢您的帮助。
【问题讨论】:
标签: ios swift sorting firebase firebase-realtime-database