iOS - Another Thread Needs to Send Reloaddata to the Mainthread

iOS - another thread needs to send reloadData to the mainthread

[self.tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO];

Do I need to use @synchronized when calling UITableView reloadData method?

You shouldn't call reloadData from threads other than the main thread.

See this similar question:

iOS - another thread needs to send reloadData to the mainthread

Reloading UITableView on Main Thread does not always work

So, the problem was that I added a prototype UITableViewCell to my UITableView in the Interface Builder (IB) of Xcode 7 Beta 4. I don't know why, but adding a prototype UITableViewCell to a UITableView in the IB caused problems in all the UITableViews. After deleting the prototype UITableViewCells again from the UITableViews in my project in IB everything works just fine. This way my code creates a couple of UITableViewCells upon launch and reuses them if I change their data and call reloadData() on the UITableView.

For an IM app, if I reloadData frequently, Is it possible the main thread be blocked? If yes, any good way to solve it?

You don't have to reload the whole table view every time, but you should only reload the section (or row) with the new message using - (void)reloadSections:(NSIndexSet *)sections or - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation.

To answer your question, if the main thread is blocked by a long operation it won't send you a memory warning but your UI will be blocked making your app unresponsive therefore impacting the user experience.

Swift UITableView reloadData in a closure

UIKit isn't thread safe. The UI should only be updated from main thread:

dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}

Update. In Swift 3 and later use:

DispatchQueue.main.async {
self.tableView.reloadData()
}


Related Topics



Leave a reply



Submit