Swift 3 Firebase更新代码转换 - 排序 [英] Swift 3 Firebase update code convertion - sorting

查看:198
本文介绍了Swift 3 Firebase更新代码转换 - 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在Udemy上注册过一门课程,关于如何使用Swift和Firebase创建一个聊天应用程序,仅仅是因为我想深入了解它是如何工作的。



但是,一个星期之后,我完成了课程。这是几个月前,从那以后 - Firebase和Swift都有了更新,现在我正在努力将一些旧的代码转换成新的代码。



这是我目前正在努力的代码:

  func loadRecents(){
$ queryOrdered(byChild:userId)。q​​ueryEqual(toValue:FIRAuth.auth()!. currentUser?.uid).observe(.value){(snapshot:FIRDataSnapshot)in b $ b firebase.child(Recent

self.recents.removeAll()

如果snapshot.exists(){

如果让values = snapshot.value as? [String:AnyObject] {

let sorted =(values as!NSArray).sortedArray(using:[NSSortDescriptor(key:date,ascending:false)])


最近在排序{

self.recents.append(最近的!NSDictionary)
self.conversationTableView.reloadData()

}



self.conversationTableView.reloadData()



self.conversationTableView.reloadData()



self.checkUnread()


$ / code $ / pre

所以这只是在tableView中显示所有聊天的代码,我只是通过Date对其进行排序,其中的升序是false。

我所做的更改只是简单地将旧的 snapshot.value.allValues 转换为 snapshot.value为? [字符串:AnyObject] 。查询按已登录账户(FIRAuth.auth()。currentUser!.uid)



不过,我非常肯定这个代码已经被弃用了,因为每次构建这个时都会出错。而且我知道它在以前的版本中有效。



这是错误:

$ p $ 线程1:EXC_BAD_INSTRUCTION

如果有必要的话,我发现当前代码已经完全搞乱了(可能是) ,这里是完整的旧代码:

  func loadRecents(){
firebase.child(Recent)。 queryOrderedByChild(userId)。q​​ueryEqualToValue(FIRAuth.auth()!. currentUser!.uid).observeEventType(.Value,withBlock:{
快照

self.recents.removeAll )
$ b $如果snapshot.exists(){

let sorted =(snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key:date,ascending :false)])

最近在排序{

self.recents.append(最近的!NSDictionary)

//添加函数也有离线访问,这也将与用户最近一起下载我们不会再创建它

firebase.child(Recent)。queryOrderedByChild(chatRoomID)。queryEqualToValue(recent [chatRoomID])。observeEventType(.Value,withBlock:{





self.tableView.reloadData()
})

self.checkUnread()

}

关于如何绕过/解决这个问题的任何想法?非常感谢您的帮助。

解决方案

我不知道Firebase结构,但是如果数据如下所示, / p>

  [
(post_42,[date:2016-09-16]),
(post_12,[date:2016-09-19]),
(post_87,[date:2016-09-04])
]

和一些代码来打印按日期排序的数据(这会在block / closure内部进行)

 让dict = snapshot!.value as! [字符串:[字符串:字符串]] 

//打印字典中的无序数据
为字典中的项目{
print(item)
}

let sortedArray = Array(dict).sorted {$ 0.1 [date]! < $ 1.1 [ 日]! }

//打印按日期排序的数据
为sortedArray中的sortedItem {
print(sortedItem)
}


I enrolled a course on Udemy a while back, on how to create a chat application with Swift and Firebase, simply because I wanted to get an insight of how it all was working.

However, I finished the course a week later. This was a couple of months ago, and since then - there has been an update to both Firebase and Swift, and I am currently struggling with converting some of the old code, to new code.

Here is the code that I am currently struggling with:

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()

    }

So this is simply the code of displaying all the "Chats" in a tableView, where I am simply sorting it through Date, where ascending is false.

The changes that I have made, is simply to convert the old snapshot.value.allValues to snapshot.value as? [String:AnyObject]. The query is sorted by the logged in account (FIRAuth.auth().currentUser!.uid).

However, I am very sure this code is deprecated, as I get an error each time I build this. And I know for a fact that it worked in the previous version.

This is the error:

Thread 1: EXC_BAD_INSTRUCTION

And if it is necessary, and it happens that I have messed up the current code completely (which might be), here is the full old code:

 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()

    }

Any ideas on how I would bypass/fix this? Help is greatly appreciated.

解决方案

I don't know the Firebase structure but here's a solution if the data looks like this:

[
  ("post_42", ["date": "2016-09-16"]),
  ("post_12", ["date": "2016-09-19"]), 
  ("post_87", ["date": "2016-09-04"])
]

and some code to print the data sorted by the date (this would go inside the block/closure)

let dict = snapshot!.value as! [String:[String:String]]

//print the unordered data from the dictionary
for item in dict {
     print(item)
}

let sortedArray = Array(dict).sorted { $0.1["date"]! < $1.1["date"]! }

//print the data sorted by date               
for sortedItem in sortedArray {
     print(sortedItem)
}

这篇关于Swift 3 Firebase更新代码转换 - 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆