I started getting crash reports after uninstall due to uninstall and was able to recreate it. Then, since I used the Apple iPhoneCoreDataRecipes example as a model, I tried the source code for the example, and also failed every time.
To recreate, just download and run the CoreDataRecipes project . Then (1) click, say, “Macaroni” to see the ingredients in the recipe, (2) swipe to remove the ingredient, and (3) swipe over the other ingredient. Crash.
My question is: am I missing an error in the sample project code, or is this really an error in the OS? And secondly, is there a way in which this scenario could be recognized and protected from it, but still allowed to delete-delete? (For now, I just turned off deletion due to deletion, returning UITableViewCellEditingStyleNone from tableView: editStyleForRowAtIndexPath: if I'm not already in edit mode.)
In a list like this, the Edit button sends setEditing: YES to the VC table, which adds the Add Element button to a new row (obviously to add new elements to the list) and the existing elements get a red minus icon to allow deletion. When done, Done sends setEditing: NO, which removes the new line and icons. Everything works perfectly. (See Example 1 in the debug file below).
If instead the user simply wants to delete one item, he can do this by checking the item (and confirm by clicking "Delete"). To make things consistent, the swipe also sends setEditing: YES to the VC table, and then the Delete button sends a commitEditingStyle: delete message and setEditing: NO message. The problem is that the system queries the number of rows in the table between the two messages, and not after setEditing: NO. Thus, he remembers that there is one more row than it really is. Then, if you do something in the table (for example, click "Edit" again, pasting the "Add" line again), it will see an inconsistent number of rows and a failure. See Case 2 at the exit below.
, , , , "" ( , "" ). setEditing: YES . . 3
=============
>>CASE 1: Using Edit control to setEditing
2013-12-29 11:02:04.032 Recipes[10287:70b] Num Rows: 4
2013-12-29 11:02:04.035 Recipes[10287:70b] Num Rows: 4
-Hit Edit
2013-12-29 11:02:14.720 Recipes[10287:70b] setEditing: On
2013-12-29 11:02:14.725 Recipes[10287:70b] Inserting Add row 4
2013-12-29 11:02:14.726 Recipes[10287:70b] Num Rows: 4 +1
2013-12-29 11:02:14.727 Recipes[10287:70b] Providing Add Cell for row 4
-Hit EditControl ("-") then Delete
2013-12-29 11:02:21.806 Recipes[10287:70b] Deleting ingredient at row 2
2013-12-29 11:02:21.808 Recipes[10287:70b] Num Rows: 3 +1
-Hit Done
2013-12-29 11:02:28.032 Recipes[10287:70b] setEditing: Off
2013-12-29 11:02:28.036 Recipes[10287:70b] Deleting Add row 3
2013-12-29 11:02:28.036 Recipes[10287:70b] Num Rows: 3
CASE 2: Using Swipe to Delete
2013-12-29 11:03:21.705 Recipes[10304:70b] Num Rows: 4
2013-12-29 11:03:21.707 Recipes[10304:70b] Num Rows: 4
-Swipe on row 2
2013-12-29 11:03:34.357 Recipes[10304:70b] setEditing: On
2013-12-29 11:03:34.358 Recipes[10304:70b] Inserting Add row 4
2013-12-29 11:03:34.359 Recipes[10304:70b] Num Rows: 4 +1
2013-12-29 11:03:34.359 Recipes[10304:70b] Providing Add Cell for row 4
-Hit Delete
2013-12-29 11:03:38.427 Recipes[10304:70b] Deleting ingredient at row 2
2013-12-29 11:03:38.431 Recipes[10304:70b] Num Rows: 3 +1
2013-12-29 11:03:38.432 Recipes[10304:70b] setEditing: Off
2013-12-29 11:03:38.434 Recipes[10304:70b] Deleting Add row 3 NOTE THAT IT DOES >>NOT<< CALL numberOfRows during this delete, but complains that current numberOfRows is wrong.
2013-12-30 23:54:26.349 Recipes[16694:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.80.1/UITableView.m:1366
2013-12-30 23:54:26.352 Recipes[16694:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. 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 (1 inserted, 0 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 0x01b48c94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018c78b6 objc_exception_throw + 44
2 CoreFoundation 0x01b48af8 +[NSException raise:format:arguments:] + 136
3 Foundation 0x005aa1ae -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00ab3463 -[UITableView _endCellAnimationsWithContext:] + 13402
5 UIKit 0x00ac313a -[UITableView endUpdatesWithContext:] + 51
6 UIKit 0x00ac3168 -[UITableView endUpdates] + 41
7 Recipes 0x00009a8c -[RecipeDetailViewController setEditing:animated:] + 908
8 UIKit 0x00cab0b5 -[UITableViewController tableView:willBeginEditingRowAtIndexPath:] + 49
9 UIKit 0x00abbb39 -[UITableView _sendWillBeginEditingForIndexPath:] + 73
10 UIKit 0x00ac772d -[UITableView _swipeCellAtPoint:] + 346
11 UIKit 0x00c44cd0 -[UITableViewCellScrollView gestureRecognizerShouldBegin:] + 354
12 UIKit 0x00d6d676 -[UIGestureRecognizer _shouldBegin] + 1309
13 UIKit 0x00d69f95 -[UIGestureRecognizer setState:] + 171
14 UIKit 0x00d9a9d7 -[UIPanGestureRecognizer touchesMoved:withEvent:] + 142
15 UIKit 0x00e4dd5a -[UIScrollViewPanGestureRecognizer touchesMoved:withEvent:] + 89
16 UIKit 0x00a1c148 -[UIWindow _sendGesturesForEvent:] + 649
17 UIKit 0x00a1d2e1 -[UIWindow sendEvent:] + 1021
18 UIKit 0x009eff32 -[UIApplication sendEvent:] + 242
19 UIKit 0x009d9ec3 _UIApplicationHandleEventQueue + 11455
20 CoreFoundation 0x01ad1f5f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01ad18eb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01aee9be __CFRunLoopRun + 910
23 CoreFoundation 0x01aee1e3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01aedffb CFRunLoopRunInMode + 123
25 GraphicsServices 0x02f5a2c2 GSEventRunModal + 192
26 GraphicsServices 0x02f5a0e9 GSEventRun + 104
27 UIKit 0x009dcabb UIApplicationMain + 1225
28 Recipes 0x0000257d main + 125
29 Recipes 0x000024f5 start + 53
)
>>CASE 3: Using Swipe to Delete but then not deleting, with "Done"
2013-12-29 11:05:20.802 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:20.805 Recipes[10319:70b] Num Rows: 4
-Swipe on row 2
2013-12-29 11:05:25.166 Recipes[10319:70b] setEditing: On
2013-12-29 11:05:25.167 Recipes[10319:70b] Inserting Add row 4
2013-12-29 11:05:25.168 Recipes[10319:70b] Num Rows: 4 +1
2013-12-29 11:05:25.168 Recipes[10319:70b] Providing Add Cell for row 4
-Hit Done
2013-12-29 11:05:27.685 Recipes[10319:70b] setEditing: Off
2013-12-29 11:05:27.686 Recipes[10319:70b] setEditing: Off
Breakpoint at beginning of setEditingAnimated shows:
#0 0x00008edb in -[RecipeDetailViewController setEditing:animated:] at /Users/hugh/Downloads/iPhoneCoreDataRecipes-2/Classes/RecipeDetailViewController.m:150
#1 0x00c90f1b in -[UITableViewController tableView:didEndEditingRowAtIndexPath:] ()
#2 0x00aadcbb in -[UITableView _sendDidEndEditingForIndexPath:] ()
#3 0x00ac4a1f in -[UITableView _endSwipeToDeleteRowDidDelete:] ()
#4 0x00abbdbb in -[UITableView _setEditing:animated:forced:] ()
#5 0x00abbf58 in -[UITableView setEditing:animated:] ()
#6 0x00c90982 in -[UITableViewController setEditing:animated:] ()
#7 0x00008f16 in -[RecipeDetailViewController setEditing:animated:] at /Users/hugh/Downloads/iPhoneCoreDataRecipes-2/Classes/RecipeDetailViewController.m:150
#8 0x00afc3ae in -[UIViewController(UINavigationControllerItem) _toggleEditing:] ()
#9 0x01853874 in -[NSObject performSelector:withObject:withObject:] ()
#10 0x009d60c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#11 0x00caac9b in -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] ()
NOTE THE RECURSIVE CALL TO setEditing:animated:
AND CONTINUING LEADS TO THE CRASH:
2013-12-29 11:05:27.688 Recipes[10319:70b] Deleting Add row 4
2013-12-29 11:05:27.689 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:27.693 Recipes[10319:70b] Deleting Add row 4
2013-12-29 11:05:27.694 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:27.694 Recipes[10319:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:1076
2013-12-29 11:05:27.697 Recipes[10319:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 4 from section 1 which only contains 4 rows before the update'
*** First throw call stack:
(
0 CoreFoundation 0x01abe5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018418b6 objc_exception_throw + 44
2 CoreFoundation 0x01abe448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x005a3fee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00aa3373 -[UITableView _endCellAnimationsWithContext:] + 3952
5 UIKit 0x00ab4caa -[UITableView endUpdatesWithContext:] + 51
6 UIKit 0x00ab4cd8 -[UITableView endUpdates] + 41
7 Recipes 0x000091fc -[RecipeDetailViewController setEditing:animated:] + 908
8 UIKit 0x00afc3ae -[UIViewController(UINavigationControllerItem) _toggleEditing:] + 73
9 libobjc.A.dylib 0x01853874 -[NSObject performSelector:withObject:withObject:] + 77
10 UIKit 0x009d60c2 -[UIApplication sendAction:to:from:forEvent:] + 108
11 UIKit 0x00caac9b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
12 libobjc.A.dylib 0x01853874 -[NSObject performSelector:withObject:withObject:] + 77
13 UIKit 0x009d60c2 -[UIApplication sendAction:to:from:forEvent:] + 108
14 UIKit 0x009d604e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
15 UIKit 0x00ace0c1 -[UIControl sendAction:to:forEvent:] + 66
16 UIKit 0x00ace484 -[UIControl _sendActionsForEvents:withEvent:] + 577
17 UIKit 0x00acd733 -[UIControl touchesEnded:withEvent:] + 641
18 UIKit 0x00a1351d -[UIWindow _sendTouchesForEvent:] + 852
19 UIKit 0x00a14184 -[UIWindow sendEvent:] + 1232
20 UIKit 0x009e7e86 -[UIApplication sendEvent:] + 242
21 UIKit 0x009d218f _UIApplicationHandleEventQueue + 11421
22 CoreFoundation 0x01a4783f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
23 CoreFoundation 0x01a471cb __CFRunLoopDoSources0 + 235
24 CoreFoundation 0x01a6429e __CFRunLoopRun + 910
25 CoreFoundation 0x01a63ac3 CFRunLoopRunSpecific + 467
26 CoreFoundation 0x01a638db CFRunLoopRunInMode + 123
27 GraphicsServices 0x02eae9e2 GSEventRunModal + 192
28 GraphicsServices 0x02eae809 GSEventRun + 104
29 UIKit 0x009d4d3b UIApplicationMain + 1225
30 Recipes 0x00001ced main + 125
31 Recipes 0x00001c65 start + 53