last day (31 days later) » 

10:13 AM
0
Q: Table View error on reloading sections - seems to be some race condition

Michał ZiobroI have some hard to solve crashes. They happen in application from time to time not in regular way. I think the problem maybe with some race conditions and synchronizations. I am using such pattern. 1) reloadData() 2) reloadSection1(), reloadSection2(), reloadSection2(), etc. 3) I have tap...

 
You shouldn't need any of those semaphores or debouncers. Always dispatch updates to your table view data source array on the main queue. The main queue is a serial dispatch queue, which means you can't update the data source at the same time as you are reloading the table from another update.
 
But If I have simultaneous reloads ex. tap button (reload table), socket.io sections refresh, etc. then in the same time I get reloadData, reloadSection1(), etc. calls Then there are requests to api and time of this request response may vary. They even can be multiple reloadSection1() at short time intervals. reloadData makes several requests and then merge data from 2 or 3 request to display in tableview. meanwhile while refreshing I am replacing data source with loading spinner to display it in section instead of real data. So I don't think that it can be possible to do on Main Queue only
What if reloadData() starts then hide table view and display spinner, in meantime reloadSection1() starts, replace section 1 with spinner then it finishes, then reloadDate gets section1 data (old) previously then merge with section2 data, update data source and then redisplay refreshing all table view. there will be more inconsistencies.
 
The only way to resolve your threading issues is to perform everything in a single, serial dispatch queue. Since UI updates have to happen on the main queue, it is the obvious choice. Updates to the data source should be pretty quick, so I can't see an issue with performing all data source and table updates on the main queue
 
Ok but in spite of this Debouncers (all at the same custom serial queue) this code startLoading(), stopLoading, startLoading(section:) stopLoading(section:) is executed on UI Thread like _uiThread(startLoading), _uiThread(stopLoading), etc. so update of data source and reload are switched to UI Main Thread (DispatchQueue.main.async { } underneath)
 
iOS is non-preemptive. Once a given piece of work starts on a queue it runs until completion. So, if you dispatch some work on the main queue that, say, updates the array and reloads a section, nothing else can run on the main queue until all of that is done. Any other work that you dispatch into the main queue will be dispatched in turn
You have to structure your code so that you achieve two things:
1. Ensure that the table data source is consistent at all times - i.e. `numberOfRows` and `numberOfSections` return values that are consistent with what you have told the tableview is happening regarding insert/delete rows/sections
2. Ensure that UI updates occur on the main thread.
Swift arrays are not thread safe - One way to update them in a thread-safe manner is to use a serial dispatch queue. Since the main queue is a serial dispatch queue and you need to ensure that the updates occur in conjunction with what you are telling the tableview is happening, the main queue is the obvious choice.
 
10:17 AM
So I think I can remove this semaphores as Main Queue is serial non-preemptive queue. and just make sure that I update data source in the same block of code that has tableView.reloadData()?
I have something like this
self.data.append(model2)
this on background serial queue
and then
_uiThread {
self.stopLoading(data: [model1, model2, .applePay])
}
 
You should also avoid reloading the entire table unless all data has been replaced. It is better just to insert/delete/reload the affected sections or rows
 
so i now gather this [model1, model2] in data array and pass to update data source in stopLoading
yes this reloadData() reloading entire tableview is usually called on viewWillAppear
and socket refresh do something like reloadSection1()
 
You need to update the array in the main queue. By performing the array updates on a different serial dispatch queue you ensure that the array update is thread safe, but those changes can occur concurrently with main queue access by the tableview and you get an inconsistency crssh
 
Ok I think I understand it. I can stick with this debouncers and background serial queue. But as I understand I should have startLoadingSection() on main thread and then change data source + tableView.reloadSection() and hava also stopLoadingSection() and here have data[2] = model2 update + tableView.reloadSection. So the code that updates datasource array + tableView.reload should be in one method that is executed on UI Main Queue. And I should never touch data source array from background
I have here such table sections reload that firstly I place in data source spinner for this reloading section, then i make api request and then i replace dat source with new data.
 
Exactly
 
10:37 AM
Do you think this tableView.beginUpdates()/endUpdates() is required if I only update section content in data source but not inserting/removing rows/sections
tableView.beginUpdates()
self.data[section] = model
tableView.reloadSections([section], with: .none)
tableView.endUpdates()
 
10:48 AM
No, I don't think you need that. You only need begin/end updates if you are inserting/removing several items and you want the animations to occur at once
 
Ok as I now look at this code I have many data source updates from background thread so this definitely can cause this inconsistency issues. I think this grouping should fix this issues
 

last day (31 days later) »