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];
});
Related
I perform a bunch of row/section inserts and deletions with insertSections, insertRows, deleteRows etc. during the startup of a view controller. The manipulations are triggered by events of a background process (of course the actual calls to the table view are performed on the main thread; also, they are wrapped in beginUpdates and endUpdates).
When I start off these row manipulations in viewWillAppear I eventually get a crash because of an inconsistency in the row data: attempt to delete row 1 from section 0 which only contains 1 rows before the update. When this happens, there actually are 2 rows in the section before the update which apparently is not correctly recognized by the table view.
However, when I start the exactly same sequence of manipulations in viewDidAppear, there's no crash and the rows animate in and out as expected.
This looks like the table view has problems with a fast-running sequence of inserts and deletes before it appears. Is this a known limitation, or do you have another explanation for this issue?
Looks like you dont need to manipulate actual UITableViewCells, you may just manipulate some kind of view models (plain objects), prepare them, and then reload UITableView with them on viewWillAppear
I have a view which shows a list of entries in a UITableView. The data for the list is an NSArray. When the user is on this view, there is an asynchronous call to a webservice to retrieve the latest entries. When I have downloaded the latest entries, I update the NSArray and then I send off a notification to the notification center. The UITableView is listening to this event and will try to reload the table.
But with this approach I get an error which is on the lines of "The object was mutated during reading".
After going through similar questions here on SO, I see the suggested approach is to remove objects and add objects at individual indexes and ask the table view to reload the data.
Is there a way to replace the array completely and ask the table view to reload the data ?
UITableView's reloadData method should do the trick.
From Apple's documentation:
Call this method to reload all the data that is used to construct the
table, including cells, section headers and footers, index arrays, and
so on. For efficiency, the table view redisplays only those rows that
are visible. It adjusts offsets if the table shrinks as a result of
the reload. The table view's delegate or data source calls this
method when it wants the table view to completely reload its data. It
should not be called in the methods that insert or delete rows,
especially within an animation block implemented with calls to
beginUpdates and endUpdates
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instm/UITableView/reloadData
if you have Updated NSArray Object
Then you can use directly following method of UITableView
[tableView reloadData]
I have an NSMutableArray _conversationsArray that basically stores IDs of all the conversations a user has in my app. Based on that value I show different number of rows in the tableView. When I delete a conversation from the data source and trigger reload of the tableView, numberOfRowsInSection method triggers and by setting a breakpoint I check the number of elements (conversations) in that array:
Everything seems to be fine, the result is one object which is great because I had two of them before the deletion. The problem is, as you can see on the far left of the screen, both rows are still visible in the tableView (even after the breakpoint) :/ The second row should have disappeared but it didn't. It did become unresponsive to touch events, but I need it gone.
Have you ever experienced a problem like this? Any help would be greatly appreciated.
--- Update ---
Could it be that it has to do something with the threads? because it seems that the table updates as it should when the thread changes :/ I might be wrong about it as it's hard to debug
It looks like you're not reloading the table view on the main thread, since -tableView:numberOfRowsInSection is called on a background thread (?). In UIKit -reloadData must be called on the main thread.
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];
});
I am using a FetchedResultsController to fetch the data for my UITableView. The data is created via actions performed on another tab (so my table may have 5 items but if I switched tabs and go back to my table, it may have more than 5 items that it should display). My table can potentially contain many rows. Right now I am using [myFetchedResultsController performFetch] in my viewDidLoad.It appears that when I create data in my other tab, when I switch back to my table tab, that new data is put into my table automatically without me perfomring [myFetchedResultsController performFetch] again. Here are my questions:
1) Does a fetchedResultsController automatically monitor the manajedObjectContext for changes and fetches the new objects if they come into existence? (This appears to be what is happening but I just want to make sure. Perhaps I have some code that is helping me do this that I forgot I put in somewhere)
2) Does the fetch performed by [myFetchedResultsController performFetch] fetch all of the objects at that time, or does it fetch only what it can fit in the view of the table and it fetches the rest later as it needs it (as you scroll in the table)? I ask because since my table can potentially have a lot of rows, it seems inefficient to fetch all the data at once if only ~12 of them will be displayed on the table at once.
EDIT: I just realized that in my FetchedResultsController delegate methods, I have
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView reloadData];
}
Am I correct in saying that a fetchedResultsController monitors for change, but will not apply it to the table unless the table is reloaded as I have done? If so, then I have another question about UITableView. Does reloading the table only reload the rows in view and then the other rows are updated as you scroll? Again I ask because if my data is very large, it seems inefficient to reload the entire table if it will reload all rows at once.
Yes, if you add a delegate
You should set the fetch request batch size when you configure the FRC because it can only load an appropriate number of items for the screen if you tell it how many that is.
You apply the changes, the FRC just collects and supplies the data. The delegate method tells you about a change. Reloading affects the whole table in terms of row count but only shows the visible rows (assuming the batch size is set appropriately).