I created a tiny app that the user can upload a post to firebase database, and then in a table view, the content of all user is shown pretty much like Instagram. I got a little problem: the user's posts are shown in an alphabetical order rather than in a order where the latest uploaded posts is shown at the beginning of the tableview.
Here is my code where I downloaded the data from firebase and view it in my tableView. Does anyone see what is wrong, or why the posts are showing in alphabetical order?
import UIKit
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
import FirebaseCore
import Firebase
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var postsTableView: UITableView!
var posts = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
self.postsTableView.delegate = self
self.postsTableView.dataSource = self
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadData() {
Database.database().reference().child("posts").observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for post in postsDictionary {
self.posts.add(post.value)
}
self.postsTableView.reloadData()
}
}
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PostTableViewCell
// Configure the cell...
let post = self.posts[indexPath.row] as! [String: AnyObject]
cell.titleLabel.text = post["title"] as? String
cell.contentTextView.text = post["content"] as? String
if let imageName = post["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.postsImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400.0
}
@IBAction func logOutButton(_ sender: Any) {
do {
try Auth.auth().signOut()
let registerSuccess = self.storyboard?.instantiateViewController(withIdentifier: "SignInVC")
self.present(registerSuccess!, animated: true, completion: nil)
}catch {
let logInAlert = UIAlertController(title: "Ops something went wrong", message: "try again", preferredStyle: .alert)
logInAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(logInAlert, animated: true, completion: nil)
}
let registerSuccess = self.storyboard?.instantiateViewController(withIdentifier: "SignInVC")
self.present(registerSuccess!, animated: true, completion: nil)
}
}
Here is a image of my struct in the database
It would be great if someone could explain why they are shown in a alphabetical order.
