简体   繁体   中英

Reload TableView More Than Once Swift

I am trying to reload a tableview in swift more than once. As shown in the code below, initially, after 0.5 seconds, the tableview "reloads" and all of the elements for the tableview from Firebase are loaded; however, after 5.0 seconds, when runTimer() is called, the tableview doesn't reload. In essence, I am trying to "reload" the data everytime something is updated in Firebase but nothing seems to work. Any help in resolving this is appreciated as I have looked all over SO and haven't had any luck.

 override func viewDidLoad() {
            super.viewDidLoad()

            checkFireBase()

            tableViewResults.delegate = self
            tableViewResults.dataSource = self

            let nibName = UINib(nibName: "TableViewCell", bundle: nil)
            tableViewResults.register(nibName, forCellReuseIdentifier: "tableCell")

            txtsearchBar.addTarget(self, action: #selector(searchRecords(_ :)), for: .editingChanged)

            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {

                self.tableViewResults.reloadData()

            }

           runTimer()
        }

   func runTimer(){
        self.timer = Timer(fire: Date(), interval: 5.0, repeats: true, block: { (Timer) in
            self.vehicleArray.removeAll()
            DispatchQueue.main.async {
                self.checkFireBase()

            }
        })

        RunLoop.current.add(self.timer!, forMode: .defaultRunLoopMode)
        self.tableViewResults.reloadData()

    }

Instead of using the timer here you can use the listeners provided by firebase, that are automatically called when data is updated in the Firebase realtime database. see this link:

https://firebase.google.com/docs/database/ios/lists-of-data

You have clarified that your checkFirebase() method is updating your data. However the self.tableViewResults.reloadData() only gets called once (when runTimer() is called) and is not called by the timer's code block.

Either put self.tableViewResults.reloadData() inside your checkFirebase() method or inside the timer's block. This will reload the tableView each time the timer is triggered.

The timer will execute the code which is present in the completion handler or completion block.

So your table update call self.tableViewResults.reloadData() in the runTimer() function call will be executed only when you call the function. To update the table you need to reload the table when you update your data source.

Try the following

func runTimer(){
        self.timer = Timer(fire: Date(), interval: 5.0, repeats: true, block: { (Timer) in
            self.vehicleArray.removeAll()
            DispatchQueue.main.async {
                self.checkFireBase()

            }
        })

        RunLoop.current.add(self.timer!, forMode: .defaultRunLoopMode)
        self.tableViewResults.reloadData()

    }

    func checkFireBase(){
        //Fetch the latest data and update the data Source of your Table view
         self.tableViewResults.reloadData()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM