How can I order by timestamp in a Firestore collection?

Viewed 131

I'm trying to order by timestamp, named 'Alpha' as a Field, but I'm hitting an error: "Unexpectedly found nil while unwrapping an optional value". I've tried following the Docs, but am still not quite sure where I'm going wrong. Any help would be greatly appreciated.

I detail my code below:

func getFollowingPosts() {
            db.collection("iAmFollowing").document(currentUserID!).getDocument { (document, error) in
                if error != nil {
                    print("ERROR")
                } else {
                    if let document = document, document.exists {
                        let followedUID = document.get("uid") as? String
                        
                        
                        self.db.collection("posts")
                            .order(by: "Alpha")
                            .whereField("uid", isEqualTo: followedUID!).getDocuments(completion: { (documents, error) in
                            for documents in documents!.documents {
                                let title = documents.get("Title") as! String
                                let content = documents.get("Content") as! String
                                let username = documents.get("username") as! String
                                let postID = documents.get("postID")
                                let counter = documents.get("counter")
                                self.titleArray.append(title)
                                self.contentArray.append(content)
                                self.usernameArray.append(username)
                                self.postIDArray.append(postID as! Int)
                                self.effectsLabelArray.append(counter as! Int)
                                print(self.titleArray)
                                self.tableView.reloadData()
                                }
                            }
                       )}
                    }
                    
                    }
                
}
1 Answers

Thanks to Kiril S., this has been solved. I needed to safely unwrap

...
self.db.collection("posts")
     .order(by: "Alpha")
     .whereField("uid", isEqualTo: followedUID!).getDocuments(completion: { (documents, error) in
        guard let documents = documents else {
            print("NO DOCUMENTS")
            return
        }
         for documents in documents!.documents {
             let title = documents.get("Title") as! String
             let content = documents.get("Content") as! String
             let username = documents.get("username") as! String
             let postID = documents.get("postID")
             let counter = documents.get("counter")
             self.titleArray.append(title)
             self.contentArray.append(content)
             self.usernameArray.append(username)
             self.postIDArray.append(postID as! Int)
             self.effectsLabelArray.append(counter as! Int)
             print(self.titleArray)
             self.tableView.reloadData()
        }
     }
...

Then, I was prompted by the console to create a Composite Index. After this, I could use .whereField and .orderBy concurrently.

Related