Reputation: 239
I'm trying to work with online data in my iPhone app. But I'm getting mad because of this:
I have object for downloading data with NSURLConnection. Method starting the work with connection (and other stuff) in separate thread - [ NSThread detachNewThreadSelector:@selector( doConnectionInNewThread ) toTarget: self withObject: nil ];
When data are loaded (connectionDidFinishLoading) I give them to my viewController. This all stuff works fine. When I use breakpoints or NSLog I have the data ready to show in UITableView. When I call reloadData, nothing happens immediately. It reloads data after maybe 2 seconds (- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is called after this delay).
BUT when I slide the tableView, it reloads data immediately. So the connection and so on works fine but it just doesn't reload the data. Why? I thought it could be due to blocked mainThread by URLConnection. But now I use it in separate thread and it is still the same...
Upvotes: 0
Views: 1444
Reputation: 51
Your tableView.reloadData
function probably doesn't run in main thread so you should use tableview.reloadData()
in DispatchQueue.main.async
to run that in the main thread.
example:
DispatchQueue.main.async {
tableview.reloadData()
}
Upvotes: 0
Reputation: 3477
Are you accessing your table view in the detached thread?
Make sure to update the UI on the main thread using:
-performSelectorOnMainThread:withObject:waitUntilDone:
Upvotes: 2