0

SOLVED: It is not about UITableViewDiffableDataSource. The problem was accessing realm from 2 different thread sequentially and not getting consistent result

One of the thread was main thread and scrolling somehow kept main thread busy and triggered race condition


Everything works if user not scrolling tableview when update is happening.

If user is scrolling or just have finger on the tableview, no animations happening on update and differences does not show up. I am not getting any error in the console

Data update code is like below:

var snapshot = tableViewDataSource.snapshot()
snapshot.deleteAllItems()
snapshot.appendSections([.conversation])
snapshot.appendItems(conversationList, toSection: .conversation)
tableViewDataSource.apply(snapshot)

Is this somehow an expected behavior?

metinn
  • 116
  • 1
  • 7
  • Why would that code run while the user "is scrolling"? – matt Aug 03 '21 at 13:14
  • while user reading and having a finger on the tableview, new message can come and i need to update. in this case i don't care about animation but the new message does not show up. Even though new state includes it – metinn Aug 03 '21 at 13:25
  • Why don't you just wait to update until the scrolling / finger isn't happening? That's my point. This isn't happening magically by itself. You are doing it. Well don't. – matt Aug 03 '21 at 13:27
  • The "workaround" fix i did, waits. But i don't think this is what i should do. Thus i am asking is this an expected behavior? if it documented anywhere it would help a lot – metinn Aug 03 '21 at 13:46

1 Answers1

1

In my experience, you have to be cognizant of when UI updates occur. UI updates always occur on the main thread.

When the user is actively scrolling, I believe this blocks the main thread, so your diffable data source is likely updating, but your app cannot update the UI until the user releases his/her finger from the display.

user1922718
  • 149
  • 1
  • 9
  • Then i expect the update happening some time later. But it never comes, unless i fetch data again. I made sure the data source update happening in main thread, so i don't think it is threading issue – metinn Aug 04 '21 at 09:16