关键信息:attempt to delete row 0 from section 0 which only contains 0 rows before the update
最近在处理刷新cell的时候有很小几率发送上面所报崩溃信息的问题,在网上找了一下信息包括腾讯bugly 分享的可能性,先贴出来看下:
解决方案
内部矛盾异常,断言评估一个条件,如果条件为 false ,调用当前线程的断点句柄。每一个线程有它自已的断点句柄,它是一个 NSAsserttionHandler 类的对象。当被调用时,断言句柄打印一个错误信息,该条信息中包含了方法名、类名或函数名。然后,它就抛出一个 NSInternalInconsistencyException 异常。
NSInternalInconsistencyException,从它的字面意思来看的话,是不一致导致的,下面就一些例子
1. NSMutableDictionary的错误使用
比如把NSDictionary当做NSMutableDictionary来使用,从他们内部的机理来说,就会产生一些错误,NSMutableDictionary中有很多NSDictionary不支持的接口
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
NSString *result = @"{\"username\”:\”aaa\”,\"phone\":\"15666666666\",\"bankcount\":\"98765432112345678\"}";
NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if (info) {
NSString *username = [Utils UrlDecode: info[@"username"]];
[info setObject:username forKey:@"username"];
}
执行上述代码后报错:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
出错原因在于:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
返回的结果是immutable对象,不能直接对immutable进行赋值操作,否则会报错。
修改后代码:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
NSString *result = @"{\"username\”:\”aaa\”,\"phone\":\"15666666666\",\"bankcount\":\"98765432112345678\"}";
NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];
//----将immutable转换为mutable----
NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:temp];
//----------------------
if (info) {
NSString *username = [Utils UrlDecode:info[@"username"]];
[info setObject:username forKey:@"username"];
}
2. 界面使用不当
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
eason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/74075F37-7B13-4D39-8686-050402501AE0/CanUSee.app> (loaded)' with name 'ViewController''
其中一个原因是:
在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中使用xib文件初始化,但程序的入口是storyboard,didFinishLaunching不需要写东西
self.viewController = [[ViewController alloc] initWithNibName: @"ViewController" bundle: nil];
工程里面没有ViewController.xib,初始化出错。
3.Constant is not finite! That's illegal. constant:nan'
除数为0时候崩溃
4. failed to obtain a cell from its dataSource
CellIdentifier I bet your cellForRowAtIndexPath is returning null.
应该避免cellForRowAtIndexPath 返回的cell为空
5.对象为nil,不存在
[MASViewConstraint setSecondViewAttribute:],nil调用约束方法导致崩溃
最终分析最符合情况的应该是3.4 这两类。
在更新 TableView 的时候调用
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
crash掉了,报异常attempt to delete row 0 from section 0 which only contains 0 rows before the update。
原因:
在调用reloadRowsAtIndexPaths时,依赖于tableView先前的状态已有要更新的cell,它内部是先删除该cell,再重新创建,所以当你在原先没有该cell的状态下调用reloadRowsAtIndexPaths,会报异常你正在尝试不存在的cell。
简言之,如果你要改变tableView的cell数量,就不要调用更新局部的cell方法。
解决办法
1.使用reloadData
reloadData是完全重新加载,包括cell数量也会重新计算,不会依赖之前tableView的状态。
2.使用beginUpdates和 endUpdates(未自验证,验证者可留言)
当你在将要更改cell数量的时候都要通知tableView更新(调用[tableView beginUpdates];)
添加时通知使用方法(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
移除时通知使用方法 (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
当你已经更改cell数量的时候都要告诉tableView更新完毕(调用[tableView endUpdates];)