大佬教程收集整理的这篇文章主要介绍了ios – 快速行插入UITableView会导致NSInternalInconsistencyException,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图了解其他人如何处理这些类型的情况.其他开发人员如何缓解两个表视图更新操作之间存在多线程竞争条件的问题?我是否应该有一个更安全的锁来控制更新通知(为什么@synchronized不执行它应该做的事情)?
任何意见是极大的赞赏!谢谢.
一些伪代码:
我的模型类有一个像这样的方法,其他线程调用它来将新行追加到表视图中:
- (void)addRow:(NSString *)data { @synchronized(self.arrayOfData) { NSInteger nexTindex = self.arrayData.count; [self.arrayData addObject:data]; [NsnotificationCenter.defaultCenter postNotificationName:kDataupdatedNotification object:self userInfo:@{@"insert": @[[NSIndexPath indexPathForRow:nexTindex inSection:0]]}]; } }
我的控制器类有一个这样的方法来接受kDataupdatedNotification通知并实际执行行插入:
- (void)onDataupdatedNotification:(Nsnotification *)notification { NSDictionary *changes = notification.userInfo; [self.tableView insertRowsATindexPaths:changes[@"insert"] withRowAnimation:UITableViewRowAnimationBottom]; }
一种解决方案是将更新排队到专用队列上,并让该队列同步更新主队列上的数据模型(我还没有测试过这段代码):
@interface MymodelClass () @property (strong,nonatomiC) dispatch_queue_t myDispatchQueue; @end @implementation MymodelClass - (dispatch_queue_t)myDispatchQueue { if (_myDispatchQueue == nil) { _myDispatchQueue = dispatch_queue_create("myDispatchQueue",null); } return _myDispatchQueue; } - (void)addRow:(NSString *)data { dispatch_async(self.myDispatchQueue,^{ dispatch_sync(dispatch_get_main_queue(),^{ NSInteger nexTindex = self.arrayData.count; [self.arrayData addObject:data]; [NsnotificationCenter.defaultCenter postNotificationName:kDataupdatedNotification object:self userInfo:@{@"insert": @[[NSIndexPath indexPathForRow:nexTindex inSection:0]]}]; }); }); }
您需要中间调度队列的原因如下.在原始解决方案(下面)中,您在主队列上获得了一系列看起来像这样的块:
>添加行N.
>添加行N 1
>由表视图发布的行N动画
>由表视图发布的行N 1动画
在步骤(3)中,动画块与表视图不同步,因为(2)首先发生,这导致异常(我认为断言失败).因此,通过从私有调度队列同步将添加行块发布到主队列,您将得到如下内容:
>添加行N.
>由表视图发布的行N动画
>添加行N 1
>由表视图发布的行N 1动画
没有举起你的工人队列.
ORIGINAL解决方案仍然存在重叠动画的问题.
如果您在主队列上更新数据模型,我认为你会没事的:
- (void)addRow:(NSString *)data { dispatch_async(dispatch_get_main_queue(),^{ NSInteger nexTindex = self.arrayData.count; [self.arrayData addObject:data]; [NsnotificationCenter.defaultCenter postNotificationName:kDataupdatedNotification object:self userInfo:@{@"insert": @[[NSIndexPath indexPathForRow:nexTindex inSection:0]]}]; }); }
以上是大佬教程为你收集整理的ios – 快速行插入UITableView会导致NSInternalInconsistencyException全部内容,希望文章能够帮你解决ios – 快速行插入UITableView会导致NSInternalInconsistencyException所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。