I am trying to update the cells in a UITableView
by calling .reloadData()
but it reloads the data before the process finishes therefore not really updating anything since the array that it gets data from is still empty. The way I have the code structured is that it makes a bunch of API calls that are each a Swift Operation
. A OperationQueue
executes these operations. What needs to happen now is when all the tasks are completed it such call self.tableView.reloadData()
. I've tried putting it in a operation's completion block however it is not the main thread and therefore does not execute.
Any help would be appreciated.
EDIT: I've tried changing the thread of the OperationQueue
to the main thread using OperationQueue.main
and adding a final operation that is run after all other operations are done that contains self.tableView.reloadData()
. Although this allows me to reload the UITable
it causes the UI to lag. (I have a pull to refresh in the UITableView
in which makes the main thread use obvious.)
Sample Code:
let operation1 = BlockOperation {
//Gets Data from server & waits until task is complete before ending
}
let operation2 = BlockOperation {
//Parse JSON
}
let updateOperation = BlockOperation {
self.tableView.reloadData()
//Throws a "UITableView.reloadData() must be used from main thread only"
}
operation2.addDependency(operation1)
updateOperation.addDependency(operation2)
let queue = OperationQueue()
queue.addOperation(operation1)
queue.addOperation(operation2)
queue.addOperation(completionOperation)