Home > Enterprise >  Updating value of tableView with Realtime Firebase Database
Updating value of tableView with Realtime Firebase Database

Time:08-02

My Code below performs following action. It connects to a Firebase Realtime Database and looks which newsfeed the user is following right now and puts it in an array. Then the code get written comments from the separate feed and combines it in one feed in a tableView.

My problem is following. When the user starts the view for the first time it loads everything correct. But as soon somethings change it observes if somethings changed and adds the changed comment to the feed. But then I have the same comment two times. The old comment and the new comment.

I need a way to update or remove the old value and show the new value. I am pretty sure I have to use self.table.removeAll() but I don't now where. I can not use it like below because this will cause that only the last feed from the array will be shown.

    func datenBankAbfrage() {
        
        for name in myFeed.myArray1[0..<myFeed.myArray1.count] {
            ref = Database.database().reference().child("placeID/\(name)")
                     
                     ref.observe(.value) { (snapshot) in
                         self.table.removeAll()
                         
                     for video in snapshot.children.allObjects as! [DataSnapshot] {
                         let Object1 = video.value as? [String: AnyObject]

                         let userName = Object1?["userName"]
                         let userGroup = Object1?["userGroup"]
                         let userComment = Object1?["userComment"]
                         
                         let video = importComment(
                             userName: userName as! String, userGroup: userGroup as! String,
                             userComment: userComment as! String)

                         self.table.append(video)
                         self.table = self.table.sorted { $0.userTime! > $1.userTime! }
                         self.tableView.reloadData()
                     }
                 }
            myFeed.myArray1 = []
        }
        
        }

CodePudding user response:

You either need to have a dictionary/struct for this

 var dic = [String:[ImportComment]]()

Then in place of

self.table.removeAll()

Put

self.dic[name] = [] // you only delete items for specific name 

And after the for video in snapsho....{} add this

self.dic[name] = specificNameVideos // you only set items to specific name

Where specificNameVideos is

var specificNameVideos = [ImportComment]()

That you fill with contents from the mentioned for loop, And any time you need all comments

var allComments = Array(dic.values).flatMap { $0 } // You can also sort allComments with userTime property

Edit:

class ViewController: UIViewController {

    var dic = [String:[ImportComment]]()
    var table = [ImportComment]()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        datenBankAbfrage()
    }

    func datenBankAbfrage() {
        
        for name in myFeed.myArray1[0..<myFeed.myArray1.count] {
            ref = Database.database().reference().child("placeID/\(name)")
                     
                     ref.observe(.value) { (snapshot) in
                         self.dic[name] = []
                         
                     var specificNameVideos = [ImportComment]()

                     for video in snapshot.children.allObjects as! [DataSnapshot] {
                         let Object1 = video.value as? [String: AnyObject]

                         let userName = Object1?["userName"]
                         let userGroup = Object1?["userGroup"]
                         let userComment = Object1?["userComment"]
                         
                         let video = importComment(
                             userName: userName as! String, userGroup: userGroup as! String,
                             userComment: userComment as! String)

                         specificNameVideos.append(video)
                     }
                     self.dic[name] = specificNameVideos
                     let tem = Array(dic.values).flatMap { $0 } // You can also sort allComments with userTime property
                     self.table = tem.sorted { $0.userTime! > $1.userTime! }
                     self.tableView.reloadData()
                 }
            myFeed.myArray1 = []
        }
        
    }
    
}
  • Related