Swift 3 Firebase update code convertion - sorting
<p>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.</p>

<p>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.</p>

<p>Here is the code that I am currently struggling with:</p>

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

    }
</code></pre>

<p>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.</p>

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

<p>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. </p>

<p>This is the error:</p>

<pre><code>Thread 1: EXC_BAD_INSTRUCTION
</code></pre>

<p>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:</p>

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

    }
</code></pre>

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