0

I am trying to reload my Tableview with new datasource every second, but there is a slight flickering in the reloading which is clearly visible to the User.

My requirement for refreshing the datasource every second is because I get the new servertime from the API after every second.

I have used the following different methods to reload my 3 sections of my tableView, but now seems to give a without flicker experience.

Method 1

 UIView.performWithoutAnimation {
                UIView.setAnimationsEnabled(false)
                weakSelf.tradeTableView.reloadSections([0,1,2], with: .none)
            }

Method 2

      UIView.setAnimationsEnabled(false)
      weakSelf.tradeTableView.beginUpdates()
      weakSelf.tradeTableView.reloadSections([0,1,2], with: .none)
      weakSelf.tradeTableView.endUpdates()
      UIView.setAnimationsEnabled(true)

Both methods are giving a flicker experience.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Samarth Kejriwal
  • 1,168
  • 2
  • 15
  • 30
  • why you need to update your sections every second? – Alastar May 07 '19 at 07:36
  • As i mentioned, that i get the updated servertime from the server every second, so thats why i need to update the tableview sections to reflect those changes. – Samarth Kejriwal May 07 '19 at 07:38
  • if you only add data you can insert rows instead of reloading all. – Alastar May 07 '19 at 07:41
  • Hi Samarth do try tradeTableView.layer.removeAllAnimations() inside performWithoutAnimation block, also I would prefer using rxswift for this kind of stuff because ur reloading tableview every second and thats not good , Hope it helps :P – Shivam Gaur May 07 '19 at 08:01
  • @ShivamGaur I got this suggestion of using RxSwift, Can you please guide me with this – Samarth Kejriwal May 07 '19 at 08:31
  • @ShivamGaur I got this suggestion of using RxSwift, Can you please guide me with this – Samarth Kejriwal May 07 '19 at 08:31
  • does your data change every second too? try using only insertRows/deleteRows specifically for items that have been changed and not reload the whole table entirely – ctomato May 07 '19 at 08:33
  • @ctomato How to figure out which row data has changed – Samarth Kejriwal May 07 '19 at 08:39
  • @SamarthKejriwal you have to read rxswift if you want to implement this effectively, On lay man terms there you will bind your data and if there is any change in data then value are dynamically altered, u dont have to reload , also please go through below link if you're new to RX - https://www.raywenderlich.com/900-getting-started-with-rxswift-and-rxcocoa also I would insist on going to youtube and look for reywenderlich reactive tutorials there, that would help :) – Shivam Gaur May 07 '19 at 09:32
  • @SamarthKejriwal As a practice visual element should not be refreshed too often as it creates a bad experience for the user and also adds extra overhead on the MainThread and processor which consumes more battery. Anyways, most of the times there should not be any flickering while reloading the cell as you have not shared code. I am assuming you are reconstructing your data models every time and reconstruction of these data models is taking time. Use time profiler to find exact root cause – Anshul May 07 '19 at 11:57

3 Answers3

0

Is it flickering or jumping?

If the scrollview jumps check these answers Jumpy tableview after reload

Soufian Hossam
  • 487
  • 5
  • 16
0

This this one

            let loc = tradeTableView.contentOffset  // this line is added to lock offset of tableview
            UIView.performWithoutAnimation {
                tradeTableView.beginUpdates()
                tradeTableView.reloadData()
                tradeTableView.endUpdates()

                tradeTableView.layer.removeAllAnimations()
                tradeTableView.setContentOffset(loc, animated: false)
            } 
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
0

you can try this

        UIView.setAnimationsEnabled(false)
        self.tradeTableView.beginUpdates()
        self.tradeTableView.endUpdates()
        UIView.setAnimationsEnabled(true)
Khush
  • 181
  • 1
  • 16