jflinter / Dwifft

Swift Diff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keep contentOffset when data has been changed

intoxicated opened this issue · comments

I've researched a lot regards to fix contentOffset after table dataSource has been updated. I couldn't find a way to do it with Dwifft. When diffCalculator.row = self.data is invoked with new values (in my case, new values will be added at front of previous data), it changes contentOffset. I attempted to use tableView.setContentOffset(offset, animated: false) after providing new data to diffCalculator which didn't work. Also, when new values (in this case, append to previous data) are added while scrolling, scroll somehow jumps on and off at some weird position. I also noticed, when same values were set to diffCalculator, it treated there were diff. I'd appreciate any advices. Thanks

Just tried performBatchUpdates branch and it didn't help.

I think that Dwifft should have method func setSectionedValues(_ values: ..., animated: Bool). This will help people using the library in case of having the same issue. I'm having the same issue, but I want using the library. I can create PR with the proposed functionality.

I also think that performBatchUpdates is reasonable to merge into master. It just uses new iOS 11 functionality.

@intoxicated it looks that solutions is easy, but hacky. Try doing the following after settings sectionedValues tableView.layer.removeAllAnimations()

@larryonoff - upon further consideration, I don't think your suggestion belongs in Dwifft - it's not, and should not be, responsible for how animations to collection views take place - that's what UICollectionViewLayout, etc is for. As a really simple example, you can get un-animated behavior today by doing something like

UIView.performWithoutAnimation {
  diffCalculator.sectionedValues = ...
}

What this thread needs a cogent explanation of how to preserve a table view's contentOffset without any visual artifacts when inserting rows/sections of variable height. I'm not really interested in pursuing any solutions until that is properly understood.

larryonoff's solution doesn't work for me, and moreover, it seems like I lose scroll contentOffset position unexpectedly when updating data while tableview has scrolled.. I'm investigating more on this topic atm tho

larryonoff's solution doesn't work for me, and moreover, it seems like I lose scroll contentOffset position unexpectedly when updating data while tableview has scrolled.. I'm investigating more on this topic atm tho

Did you find any solution?

One of the possible solution - perform update in [.default] runloop mode
RunLoop.current.perform(inModes: [.default]) { /* Perform update */ }
But in this approach your batchUpdates will be async. In my unusual case its not the best solution

@danya61 I don't use Dwifft anymore. I switched to DifferenceKit

@danya61 I don't use Dwifft anymore. I switched to DifferenceKit

Well, I suppose that the issue doesn't associated with the especial library, I've reproduce it even without diff framework . I've got an error when if your content offset < 0 and RunLoop mode is tracking, then after insert section (at least) content offset becomes forced zero

@danya61 hmmmm. I don't remember that I've faced with the current issue. But this not 100%

I had the same issue, and resolved with setting collectionView.contentInsetAdjustmentBehavior = .never

For UICollectionView you might use StableCollectionViewLayout.
This issue is normal behaviour for UITableView/UICollectionView.