Why do table cells appear with delay? - ios

I have a tableviewcontroller. In viewdidload, I call a method which gets some information in josn format from a backend. After receiving my desired response and serialisation, I reload the tableview.
My problem is that the content of the cells will appear within about 30 second delay than table view.
Does any one know how can I fix it?

Have you tried reloading tableView on main thread?
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

Related

Refresh tableviewcontroller in ios

I am trying to refresh the table view controller when certain task is performed inside a block.I have checked questions on stackoverflow but they deal with refresh option but i want it to just reload view and go to viewDidLoad and ViewWillAppear.This was the suggestions.
[self.tableView reloadData];
but it doesn't seem to work.Any ideas?
You can not reload tableview or update any view inside a block. If you want to update or refresh your table then you will have create a main threaded block and write your code for table refresh on the main thread.
So, try this code:
dispatch_async(dispatch_get_main_queue(), ^{
enter code here
});

iOS UITableView behaviour broken when not active

My current set up is the following:
Root Tab Bar:
Collection view with magazines
Bookmarks (with a table view)
Others
You can add a bookmark from a magazine in the collection view and also remove it from there.
The behaviour I'm seeing is the following:
I start the application, the table view queries the number of sections, number of cells, but not the cellForRowAtIndexPath. I could understand why, as there is no cell in the active view, so no data should be loaded.
When I add a bookmark from the collection view, it adds it to the array (via a notification) and requests the tableview to be reloaded. As there isn't an initial entry, it goes through the motions described above. When I press it again to remove the bookmark the entry is removed from the array. This is where it gets interesting because the first thing the table calls is not the number of sections or rows but the cellForRowAtIndexPath. As the array is empty, the application crashes on a request for data on index 0.
My question is why does the cell creation get called in that order? Is there any way to avoid it?
If you changed the section, try calling - (void)reloadSections:(NSIndexSet *)sections before you call reloadData
https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UICollectionView_class/index.html#//apple_ref/occ/instm/UICollectionView/reloadSections:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
The reason this was happening is because I was attempting to change something about the table before I reloaded the data.
[self.tableView setSeparatorStyle: !_helpText.hidden ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
That was for removing the lines so a message can be displayed. However that update was using old data as reloadData had not been called.
[self.tableView reloadData];
[self.tableView setSeparatorStyle: !_helpText.hidden ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine];
Reversing them fixed the issue.

Why crashes UISearchBar after reload TableView

I have tableView with UISearchBarDisplayController. The below code was fetch record from webserver and display the tableview cell every works fine. but when will scroll 3 or 5 time downwards and upwards through i will going to click search bar it will crash the crash report NSLog doesn't print anything.
Remember i tried NSZombie , breaking point,exception point etc., but nothing helps.
Important question:
How to load data to tableview after fetched data from webserver. because tableview delegates methods are called before fetching data from server.
DataFetchController *getDataFetch=[[DataFetchController alloc]init];
//getleadFetch.delegate=self;
[getDataFetch beginTaskWithCallbackBlock:^(NSArray *mm){
[self.tableView reloadData];//if i comment this line the UISearchBar not crashes
}];
Loading the tableview before the data fetch is complete will obviously result in a crash.
So to avoid that we need to know when the data fetch is complete.
This can be easily obtained from your sql apis which return an error code when the fetch is complete. You can via that to find out if the fetching is complete or not.So sequentially you can provide the reloading of your table view command.
Hope it helps!!
update tableview data in main thread it not crash.
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

UITableView weird reloadData issue

I am stuck in a really weird issue, What em doing is calling NSURLConnection sendAsynchronousRequest in which i reload tableView and it does it without any issue. This scenario works fine while em standing on the same ViewController while request response is received. The issue arises when I call sendAsynchronousRequest and move from that ViewController and then come back to it before the request gives the response. This time the TableView reloadData gets called but doesn't do anything on the View. I call the reloadData in this dispatch_async(dispatch_get_main_queue(), ^{}); for it to run on mainThread.
Any help in this regard would be great.
Edit 1:
Here's what i have gotten far till now.. when i start the NSURLConnection asyncRequest it keeps the reference to the tableView and array that it has at that current time.. But when i navigate off from that view and then come back the view is reinitialized so the tableView and array has new references. So when the asyncRequest calls tableView ReloadData, it calls reload for the older referenced tableView rather than the one that is now visible hence not reloading the tableView… hope now you can help.
Rather than reloading tableview on main thread, try to reload it once you get data and the data is parsed. Switching between views may cause your tableview to reload before the data comes from server resulting blank tableview and this is caused due to calling some methods on main thread.
EDIT: Try to initialize tableview in viewdidload only and array just after you get response from server and before loading data in it.

UICollectionView Reloading Data

A question regarding reloading data in your UICollectionView.
When using reloadData it only does this once any scrolling has finished. Then there is a small pause where the collection view freezes and cannot be interacted with until the new data is loaded in. How can I get around this?
For example, I would have a view comprised of maybe 10/20/30 items, each with an image and label. I then update my dictionary with new images and strings and then reload the view to add these new items.
Many thanks.
EDIT:
On closer inspection, its not the reloading that causes the pause, its some other code, where I fetch some data. Why might this freeze my view though when i am not even reloading it to make changes to it?
I am fetching this data from -(void)scrollViewDidScroll:(UIScrollView *)scrollView
EDIT 2:
Ok here is some basic code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self myMethodWithCompletionBlock:^(NSMutableDictionary *results, NSString *nextPageToken) {
//No code in completion block is ever called when the method is called in the dispatch block! Regardless of what it is.
}
});

Resources