Why crashes UISearchBar after reload TableView - ios

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];
});

Related

Why do table cells appear with delay?

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];
});

I need to delay the call of UITableView until other methods have finished executing

I am building an ios app, that uses google places api to locate certain places and then display the details in a table view. But the problem, that I am facing is that
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
method gets called before the data acquired from the api is stored in the memory. So it (obviously) returns 0, and details are not displayed. Is there some way I can delay the call of Table View?
Well your only option is to not show the table view until there is data there.
Assuming this is not viable, why not just show an activity popup indicating that you are loading information and then when its loaded, drop the activity indicator and call [self.tableview reloadData];
You can reload your Table by below line,
[tableView reloadData];
You can make the tableview invisible and make it visible right before reloading the table.

App getting crash while reloading the data

My tableview is getting reloaded and gets new data for every 10secs by running timer in background thread.But when data is getting reloaded and the same time when I'm scrolling the tableview .
App is getting crashed.leaving the error as
[__NSArrayM objectAtIndex:]: index 18 beyond bounds [0 .. 15].
You need to remove objects from your array when you get the response. I guess you are removing your array first and then your timer is executing.
You haven't provided any code, so this is a it of a guess, but as others have said it seems likely that you are reloading your data directly into the array that is your table data source. This is causing a timing related issue where your data source array and the information you have provided to the table view previously (specifically number of rows) is inconsistent.
You should fetch your new data into a different array and then once all data has been fetched you can update the reference to your table data source array and reload the table view.
Another approach is to compare the two arrays to determine which rows have been deleted and which rows are new. You can then call delete/insert rows as appropriate. Make sure you call beginUpdates before you start adding/deleting rows and endUpdates once you have finished.
Please try to reload your table in main thread. Like below code
dispatch_async(dispatch_get_main_queue(), ^{
[self.collection 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