I am new to programming and currently trying to make a newsfeed like app. My goal is it to notice changes in the firebase database through my app and new posts in my table view. Currently I am able to show one post in my tableView. Its only changing though when you reopen the ViewController. One of my problem is that I don't know how to use any other command then obserSingleEvent and I am pretty sure that one of my main mistakes. So my questions are: 1. How do I make the tableView instantly reload when a change appears? 2. how do I display more then one post? (3. how can I show the most recent posts first?)
class ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var newsfeedTableView: UITableView!
var ref: DatabaseReference!
var posts = [String]()
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
ref = Database.database().reference()
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: { snapshot in
let newPost = snapshot.value as? String
self.posts.append(newPost!)
print("newPost: \(newPost)")
print("posts: \(self.posts)")
self.newsfeedTableView.reloadData()
print("reloadData")
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (posts.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = posts[indexPath.row]
print("posts in cell: \(posts)")
return cell
}
}

