crash on deleteRowsAtIndexPaths - iphone

My application crashes when I'm deleting row from table. Here is my sources where the bug is detected and stack trace. Thanx!
//delete row from database
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"\ncommitEditingStyle");
//delete user from Users table with specified ID
if(editingStyle == UITableViewCellEditingStyleDelete)
{
//Get the object to delete from the array.
[dbManager open: #DB_FILE_NAME];
//get userID from array usersIDList[row] = userID!
NSNumber *usersID = [usersIDList objectAtIndex: [indexPath row]];
[dbManager deleteUserWithID: [usersID intValue] table: #TABLE_USERS fieldName: #"UserID" ];
[dbManager close];
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject: indexPath]
withRowAnimation: UITableViewRowAnimationFade];
}
}
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
NSLog(#"\ndeleteRowsAtIndexPaths");
}
[Session started at 2010-11-15 18:01:23 +0200.]
2010-11-15 18:01:25.052 PhoneBook[12397:207] ******* Accessibility Status Changed: On
2010-11-15 18:01:25.101 PhoneBook[12397:207] ********** Loading AX for: com.yourcompany.PhoneBook ************
2010-11-15 18:01:30.060 PhoneBook[12397:207]
deleteBtnUserClick
2010-11-15 18:01:31.878 PhoneBook[12397:207]
commitEditingStyle
2010-11-15 18:01:31.881 PhoneBook[12397:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920
2010-11-15 18:01:31.883 PhoneBook[12397:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0 CoreFoundation 0x02510b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0266040e objc_exception_throw + 47
2 CoreFoundation 0x024c9238 +[NSException raise:format:arguments:] + 136
3 Foundation 0x000b8e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00338d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719
5 UIKit 0x00328511 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
6 UIKit 0x0aaca8fc -[UITableViewAccessibility(Accessibility) deleteRowsAtIndexPaths:withRowAnimation:] + 74
7 PhoneBook 0x00003491 -[RootViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 392
8 UIKit 0x0032596d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
9 UIKit 0x0aacde92 -[UITableViewCellAccessibility(SafeCategory) deleteConfirmationControlWasClicked:] + 62
10 UIKit 0x002be7f8 -[UIApplication sendAction:to:from:forEvent:] + 119
11 UIKit 0x00349de0 -[UIControl sendAction:to:forEvent:] + 67
12 UIKit 0x0034c262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
13 UIKit 0x0034ae0f -[UIControl touchesEnded:withEvent:] + 458
14 UIKit 0x002e23d0 -[UIWindow _sendTouchesForEvent:] + 567
15 UIKit 0x002c3cb4 -[UIApplication sendEvent:] + 447
16 UIKit 0x002c89bf _UIApplicationHandleEvent + 7672
17 GraphicsServices 0x02d6c822 PurpleEventCallback + 1550
18 CoreFoundation 0x024f1ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
19 CoreFoundation 0x02452807 __CFRunLoopDoSource1 + 215
20 CoreFoundation 0x0244fa93 __CFRunLoopRun + 979
21 CoreFoundation 0x0244f350 CFRunLoopRunSpecific + 208
22 CoreFoundation 0x0244f271 CFRunLoopRunInMode + 97
23 GraphicsServices 0x02d6b00c GSEventRunModal + 217
24 GraphicsServices 0x02d6b0d1 GSEventRun + 115
25 UIKit 0x002ccaf2 UIApplicationMain + 1160
26 PhoneBook 0x000026ec main + 102
27 PhoneBook 0x0000267d start + 53
)
terminate called after throwing an instance of 'NSException'

I've seen this before and it is definitely that you have forgotten to update source of data which fill up table. But that part of code is missing.
The method which caused this is
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
}

Yes, the problem was in method numberOfRowsInSection
So, to leave troubles you should:
In function commitEditingStyle delete data from your array, database etc.
Decrement your current row count.
[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
And all be OK!

Updated Swift 4.2
If you are about to remove last row in section, you should remove entire section instead of row. To do that:
tableView.beginUpdates()
dataArray.remove(at: row)
if tableView.numberOfRows(inSection: section) == 1 {
tableView.deleteSections(IndexSet(integer: section), with: .automatic)
}
else {
tableView.deleteRows(at: indexPath, with: .fade)
}
tableView.endUpdates()

Pretty clear from the error message:
'Invalid update: invalid number of
rows in section 0. The number of rows
contained in an existing section after
the update (6) must be equal to the
number of rows contained in that
section before the update (6), plus or
minus the number of rows inserted or
deleted from that section (0 inserted,
1 deleted).'
The object you are trying to delete doesn't get deleted in your datasource.
If I assume that your UITableViewDataSource uses dbManager too, you have to fix dbManager to actually delete the object.

I got the same error and reason was I delete the last row in the table and
my numberOfSectionsInTableView implementation return rowsArray count which was 0..
changing to minimum of 1 solve the case

Related

Swift delete row with animation

Unfortunately, I have to ask this question again, because I have not found a solution yet.
At the moment I can delete it without animation, but now I want to delete it WITH animation.
My app get an error with this code:
/*************** TABLE VIEW DELETE LEBENSMITTEL ***************/
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == .Delete) {
let LM_ITEM = lebensmittel[indexPath.row]
managedObjectContext!.deleteObject(lebensmittel[indexPath.row])
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.DatenAbrufen()
}
}
2015-08-28 09:27:27.475 [32099:346567] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623
2015-08-28 09:27:27.483 [32099:346567] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
0 CoreFoundation 0x00000001091cdc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010b126bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001091cdaca +[NSException raise:format:arguments:] + 106
3 Foundation 0x00000001098ac98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000109f37c13 -[UITableView _endCellAnimationsWithContext:] + 12678
5 UIKit 0x0000000119d2937b -[UITableViewAccessibility deleteRowsAtIndexPaths:withRowAnimation:] + 48
6 App Name 0x00000001087ca640 _TFC12App_Name26AlteLebensmittelController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 3360
7 App Name 0x00000001087ca887 _TToFC12App_Name26AlteLebensmittelController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87
8 UIKit 0x0000000109f5d1e6 -[UITableView animateDeletionOfRowWithCell:] + 132
9 UIKit 0x0000000109f3c3bd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72
10 UIKit 0x0000000109e5bd62 -[UIApplication sendAction:to:from:forEvent:] + 75
11 UIKit 0x0000000109f6d50a -[UIControl _sendActionsForEvents:withEvent:] + 467
12 UIKit 0x0000000109f6c8d9 -[UIControl touchesEnded:withEvent:] + 522
13 UIKit 0x0000000109ea8958 -[UIWindow _sendTouchesForEvent:] + 735
14 UIKit 0x0000000109ea9282 -[UIWindow sendEvent:] + 682
15 UIKit 0x0000000109e6f541 -[UIApplication sendEvent:] + 246
16 UIKit 0x0000000109e7ccdc _UIApplicationHandleEventFromQueueEvent + 18265
17 UIKit 0x0000000109e5759c _UIApplicationHandleEventQueue + 2066
18 CoreFoundation 0x0000000109101431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x00000001090f72fd __CFRunLoopDoSources0 + 269
20 CoreFoundation 0x00000001090f6934 __CFRunLoopRun + 868
21 CoreFoundation 0x00000001090f6366 CFRunLoopRunSpecific + 470
22 GraphicsServices 0x000000010de80a3e GSEventRunModal + 161
23 UIKit 0x0000000109e5a8c0 UIApplicationMain + 1282
24 App Name 0x00000001087ebb67 main + 135
25 libdyld.dylib 0x000000010b868145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The number of rows to show in your tableview is not the same you got in your datasource. Deleting a row with an animation take some time so when the deleting animation end try refreshing the tableview with yourTableview.reloadData() and remove the unused data from your datasource (before the refresh).
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourDataSource[section].count
}
Here an exemple in Objective-C:
- (void)removeRowAtIndex:(NSInteger)aIndex
{
//Set the row to remove from the tableView
NSMutableArray * tRemove = [NSMutableArray array];
NSIndexPath * tIndexPath = [NSIndexPath indexPathForRow:aIndex inSection:0];
[tRemove addObject:tIndexPath];
//Remove the deleted data from the datasource
NSMutableIndexSet * tRemoveIndexSet = [NSMutableIndexSet indexSet];
[tRemoveIndexSet addIndex:aIndex];
[YourDataSource removeObjectsAtIndexes:tRemoveIndexSet];
//Remove the row from tableView
[YourTableView deleteRowsAtIndexPaths:tRemove withRowAnimation:UITableViewRowAnimationLeft];
}
Swift version:
func removeRowAtIndex(aIndex:Int) {
//Set the row to remove from the tableView
var tRemove:Array<NSIndexPath> = Array()
let tIndexPath:NSIndexPath = NSIndexPath(forRow: aIndex, inSection: 0)
tRemove.append(tIndexPath)
//Remove the deleted data from the datasource
var tRemoveIndexSet:NSMutableIndexSet = NSMutableIndexSet()
tRemoveIndexSet.addIndex(aIndex)
YourDataSource.removeAtIndexes(tRemoveIndexSet)
//OR use removeAtIndex()
//YourDataSource.removeAtIndex(aIndex)
//Remove the row from tableView
YourTableView.deleteRowsAtIndexPaths(tRemove, withRowAnimation: .Left)
}
Add this extension if you want to use removeAtIndexes()
extension Array
{
mutating func removeAtIndexes(indexes: NSIndexSet) {
for var i = indexes.lastIndex; i != NSNotFound; i = indexes.indexLessThanIndex(i) {
self.removeAtIndex(i)
}
}
}
Source : removeObjectsAtIndexes for Swift arrays
Hope this can help you :)

Can't delete row in section

i've got a problem by deleting cells in a section.The tableViewController has three sections with various cells. If I try to delete one cell, the debugger will show:
2015-01-23 20:22:15.105 Grade - Zensurenverwaltung[23854:5674475] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1566
2015-01-23 20:22:15.133 Grade - Zensurenverwaltung[23854:5674475] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).'
*** First throw call stack:
(
0 CoreFoundation 0x00000001008bef35 exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001025adbb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001008bed9a +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000100d565df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x00000001013c98ff -[UITableView _endCellAnimationsWithContext:] + 10935
5 Grade - Zensurenverwaltung 0x00000001000fa0c2 _TFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 3618
6 Grade - Zensurenverwaltung 0x00000001000fa207 _TToFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87
7 UIKit 0x00000001013edcb4 -[UITableView animateDeletionOfRowWithCell:] + 130
8 UIKit 0x00000001013ce125 __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72
9 UIKit 0x00000001012f68be -[UIApplication sendAction:to:from:forEvent:] + 75
10 UIKit 0x00000001013fd410 -[UIControl _sendActionsForEvents:withEvent:] + 467
11 UIKit 0x00000001013fc7df -[UIControl touchesEnded:withEvent:] + 522
12 UIKit 0x00000001016a3540 _UIGestureRecognizerUpdate + 9487
13 UIKit 0x000000010133bff6 -[UIWindow _sendGesturesForEvent:] + 1041
14 UIKit 0x000000010133cc23 -[UIWindow sendEvent:] + 667
15 UIKit 0x00000001013099b1 -[UIApplication sendEvent:] + 246
16 UIKit 0x0000000101316a7d _UIApplicationHandleEventFromQueueEvent + 17370
17 UIKit 0x00000001012f2103 _UIApplicationHandleEventQueue + 1961
18 CoreFoundation 0x00000001007f4551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
19 CoreFoundation 0x00000001007ea41d __CFRunLoopDoSources0 + 269
20 CoreFoundation 0x00000001007e9a54 __CFRunLoopRun + 868
21 CoreFoundation 0x00000001007e9486 CFRunLoopRunSpecific + 470
22 GraphicsServices 0x00000001052fd9f0 GSEventRunModal + 161
23 UIKit 0x00000001012f5420 UIApplicationMain + 1282
24 Grade - Zensurenverwaltung 0x00000001001e164e top_level_code + 78
25 Grade - Zensurenverwaltung 0x00000001001e168a main + 42
26 libdyld.dylib 0x0000000102d87145 start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
And i just don't get it...
Here is my code:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var indexes: NSMutableIndexSet = NSMutableIndexSet()
dataTestType = context.executeFetchRequest(fetchRequestForTestType, error: nil) as [TestType]
if editingStyle == UITableViewCellEditingStyle.Delete {
context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)
context.save(nil)
dataTestType.removeAtIndex(indexPath.row)
if dataTestType.count == 0 {
indexes.addIndex(indexPath.section)
}
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.deleteSections(indexes, withRowAnimation: .Fade)
tableView.endUpdates()
}
}
Can anyone help me?
Thank You very much!
You remove an object at indexPath.row from the dataTestType array
dataTestType.removeAtIndex(indexPath.row)
And on the next line you try to get the object at the same index, but it won't exist if you deleted the only object in the array:
context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)
You can simply solve the issue by reordering these two lines.
As matt pointed out, you can also use NSFetchedResultsController, as it is recommended for showing Core Data records in UITableView. Then you won't need dataTestType array at all. Now you call context.executeFetchRequest each time commitEditingStyle method is called. It is inefficient and can result in poor performance if you have many records to fetch.

Removing Objects from UITableView

So I've seen similar topics but I don't quite understand why I am crashing still....
I am reading entries into an array called "entries." After a while I want to add new entries and then delete the old ones.
So I'm essentially adding stuff to 'entries" and then wanting to delete some old entries. I then run the below method and dies out when it starts editing the view. The error is posted below the code block.
Thanks for the help!
-(void) removeOldEntries: (int) numOfEntries
{
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init] ;
// Remove the first number of entries in the table view. This number is specified by the numOfEntries
for (int i = numOfEntries - 1; i >= 0; i = i -1 )
{
[entries removeObjectAtIndex:i];
}
// Build deleteIndexPaths
for (int i = numOfEntries - 1; i >= 0; i = i - 1)
{
// Add objects to our index pathes array (of things we need to delete) and then remove the objects from feeds array
[deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
// Start the editing of the TableView
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
// End the editing of the table view
[deleteIndexPaths removeAllObjects];
[deleteIndexPaths release];
}
Error message:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-12-27 22:43:04.490 v1.0[999:6003] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e3dbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f925c2 objc_exception_throw + 47
2 CoreFoundation 0x00df6628 +[NSException raise:format:arguments:] + 136
3 Foundation 0x000d847b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x0035aa0f -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8424
5 UIKit 0x0034a433 -[UITableView endUpdates] + 42
6 v1.0 0x000050e6 -[NewsTableViewController removeOldArticles:] + 408
7 v1.0 0x00004d16 -[NewsTableViewController pullAndParseData] + 696
8 CoreFoundation 0x00dae67d __invoking___ + 29
9 CoreFoundation 0x00dae551 -[NSInvocation invoke] + 145
10 Foundation 0x000ff555 -[NSInvocationOperation main] + 51
11 Foundation 0x0006dbd2 -[__NSOperationInternal start] + 747
12 Foundation 0x0006d826 ____startOperations_block_invoke_2 + 106
13 libSystem.B.dylib 0x96653a24 _dispatch_call_block_and_release + 16
14 libSystem.B.dylib 0x96645cf2 _dispatch_worker_thread2 + 228
15 libSystem.B.dylib 0x96645781 _pthread_wqthread + 390
16 libSystem.B.dylib 0x966455c6 start_wqthread + 30
)
terminate called after throwing an instance of 'NSException'
You are making this needlessly complicated. There is some inconsistency in your deletion which is why there is an error. Make sure in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return for section 0
return [entries count];
}
And change your removeOldEntries: to
-(void) removeOldEntries: (int) numOfEntries
{
[entries removeObjectsInRange:NSMakeRange(0, numOfEntries-1)];
[self.tableView reloadData];
}
Use [_tableView reloadData]. This will simplify your life.
By the way, the exception you are getting is indicating that your array is not containing what you think it should. Trying NSLogging your array.

iPhone - Multithreading in Tableview - feasible approach?

I have been tracking a nasty bug recently of which I knew it was occurring due to my multithreaded approach (I add the crashreport at the end).
In my App I load a UITableView and fill it with data I store using Coredata. This data in turn I retrieve from a webservice, thus it might take some time, depending on my connection.
Anyway, I have managed to track it down a little and I know the issue is because an array is empty though I thought it shouldn't be. Then, when
numberOfRowsInSection:(NSInteger)section
is called the program crashes - but only sometimes!!!
I was wondering how to debug this properly and my approach was to call
reloadData
on the tableView after the view finishes loading. But appearantly
- (void)viewDidLoad
but of course this didn't help. Is the correct way here to create another thread and have it check repeatadly whether the data is ready and prepared? Any suggestions/opinions?
2011-01-19 21:50:49.605 myApp[2017:307] *** Terminating app due to uncaught exception
'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x314d0987 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x319a149d objc_exception_throw + 24
2 CoreFoundation 0x31462795 -[__NSArrayM objectAtIndex:] + 184
3 myApp 0x000092f7 -[MyTableViewController tableView:numberOfRowsInSection:] + 106
4 UIKit 0x33902bcf -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1338
5 UIKit 0x33903529 -[UITableViewRowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 120
6 UIKit 0x33902645 -[UITableViewRowData numberOfRows] + 96
7 UIKit 0x3390207b -[UITableView noteNumberOfRowsChanged] + 82
8 UIKit 0x33901bff -[UITableView reloadData] + 582
9 UIKit 0x33904a0b -[UITableView _reloadDataIfNeeded] + 50
10 UIKit 0x33904e63 -[UITableView layoutSubviews] + 18
11 UIKit 0x338b10cf -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 26
12 CoreFoundation 0x3146ebbf -[NSObject(NSObject) performSelector:withObject:] + 22
13 QuartzCore 0x30a6c685 -[CALayer layoutSublayers] + 120
14 QuartzCore 0x30a6c43d CALayerLayoutIfNeeded + 184
15 QuartzCore 0x30a6656d _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 212
16 QuartzCore 0x30a66383 _ZN2CA11Transaction6commitEv + 190
17 QuartzCore 0x30a70e4f _ZN2CA11Transaction5flushEv + 46
18 QuartzCore 0x30a6db75 +[CATransaction flush] + 24
19 UIKit 0x338e803f -[UIApplication _reportAppLaunchFinished] + 30
20 UIKit 0x338d6317 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 462
21 UIKit 0x338a248b -[UIApplication handleEvent:withNewEvent:] + 1114
22 UIKit 0x338a1ec9 -[UIApplication sendEvent:] + 44
23 UIKit 0x338a1907 _UIApplicationHandleEvent + 5090
24 GraphicsServices 0x35d66f03 PurpleEventCallback + 666
25 CoreFoundation 0x314656ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
26 CoreFoundation 0x314656c3 __CFRunLoopDoSource1 + 166
27 CoreFoundation 0x31457f7d __CFRunLoopRun + 520
28 CoreFoundation 0x31457c87 CFRunLoopRunSpecific + 230
29 CoreFoundation 0x31457b8f CFRunLoopRunInMode + 58
30 UIKit 0x338d5309 -[UIApplication _run] + 380
31 UIKit 0x338d2e93 UIApplicationMain + 670
32 myApp 0x000029bf main + 70
33 myApp 0x00002974 start + 40
)
terminate called after throwing an instance of 'NSException'
numberOfRows looks like:
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(#"number of rows in section");
if (section == mySection) {
return [[articleArrays objectAtIndex:section] count];
} else {
return 0;
}
}
From what you describe here your process that performs the download, or more precisely an event callback should be the only place calling reload data. calling it after adding new data to the table dataSet.
returning a 0 in numberOfRowsInSection does not cause a crash but if you are trying to get a nested array when the count is 0, this will crash. Post your numberOfRowsInSection method for us to gawk at.
EDIT:
Sounds/Looks like background thread is mutating the array while its trying to do an object count.
user210504 is right in saying that synchronising the array is likely stop this.
Typically you would sync writes and therefore preventing numberOfRowsInSection from counting if your background thread is mutating it. eg:
-(void)downloadFinished{
NSMutableArray * array = [[NSArray alloc] init];//t
id * obj;//obj is your row data object.
#synchronized(ar)
{
[array addObject:obj];//ar is your dataSet
}
[array release];
}
One thing that you can do is to encapsulate accesses to the Mutable Array in #synchronized block. This way you will be sure that you are not accessing the datastructure half way through. And then I also agree with what Luke just mentioned.

iOS Application Terminates After Exception Thrown in UITableViewDataSource Method

Having meticulously followed the examples and instructions in the Table View Programming Guide for iOS about the proper order in which UITableView delegate and data source methods are called, I thought I had a good idea of how to implement the “handshake” shown in Figure 7-1 and the list that follows, but apparently not.
Here's the code I'm using…
- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
NSLog(#"Removing %# row %d.", [dataModel objectAtIndex: indexPath.row], indexPath.row);
[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil] withRowAnimation: UITableViewRowAnimationFade];
[dataModel removeObjectAtIndex: indexPath.row];
}
tableView is the UITableView
dataModel is an NSArray instance that holds the objects represented by the UITableViewCells from the tableView.
…and the error I'm getting…
2010-08-27 21:53:17.971 SportWatch[1299:307] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1262.58/UITableView.m:920
2010-08-27 21:53:17.992 SportWatch[1299:307] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0 CoreFoundation 0x35411ed3 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x311f0811 objc_exception_throw + 24
2 CoreFoundation 0x35411d15 +[NSException raise:format:arguments:] + 68
3 Foundation 0x332b932f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 62
4 UIKit 0x338dcda1 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 4524
5 UIKit 0x338d586d -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 204
6 UIKit 0x338d5775 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 20
7 SportWatch 0x000060e7 -[MainViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 670
8 UIKit 0x338d3c3f -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 58
9 UIKit 0x33984071 -[UITableViewCell(UITableViewCellInternal) deleteConfirmationControlWasClicked:] + 28
10 CoreFoundation 0x353b9719 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
11 UIKit 0x33831d59 -[UIApplication sendAction:to:from:forEvent:] + 84
12 UIKit 0x33831cf9 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
13 UIKit 0x33831ccb -[UIControl sendAction:to:forEvent:] + 38
14 UIKit 0x33831a1d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
15 UIKit 0x3383206b -[UIControl touchesEnded:withEvent:] + 342
16 UIKit 0x338309f5 -[UIWindow _sendTouchesForEvent:] + 368
17 UIKit 0x3383036f -[UIWindow sendEvent:] + 262
18 UIKit 0x3382ae13 -[UIApplication sendEvent:] + 298
19 UIKit 0x3382a74b _UIApplicationHandleEvent + 5110
20 GraphicsServices 0x3171a04b PurpleEventCallback + 666
21 CoreFoundation 0x353a6ce3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
22 CoreFoundation 0x353a6ca7 __CFRunLoopDoSource1 + 166
23 CoreFoundation 0x3539956d __CFRunLoopRun + 520
24 CoreFoundation 0x35399277 CFRunLoopRunSpecific + 230
25 CoreFoundation 0x3539917f CFRunLoopRunInMode + 58
26 GraphicsServices 0x317195f3 GSEventRunModal + 114
27 GraphicsServices 0x3171969f GSEventRun + 62
28 UIKit 0x337d148b -[UIApplication _run] + 402
29 UIKit 0x337cf69f UIApplicationMain + 670
30 SportWatch 0x00002b23 main + 70
31 SportWatch 0x00002ad8 start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
kill
Current language: auto; currently objective-c
quit
remove the object from the data set before deleting the row.