我遇到了NSArrayController rearrangeObjects函数的问题--这个函数是从一些背景数据中调用的,有时应用程序会出错:'endUpdates调用时没有启动更新‘。
如何检测arrayController是否正在当前时刻重新排列对象,并将下一次重新排列添加到类似队列或取消当前重新排列并运行新的对象中?
这个问题可能还有别的解决办法吗?
编辑代码添加:
TableController implementation:
- (void)setContent{//perfoms on main thread
//making array of content and other functions for setting-up content of table
//...
//arrayController contains objects of MyNode class
//...
//end of setting up. Call rearrangeObjects
[arrayController rearrangeObjects];
}
- (void)updateItem:(MyNode *)sender WithValue:(id)someValue ForKey:(NSString *)key{
[sender setValue:someValue forKey:key];
[arrayController rearrangeObjects];//exception thrown here
}
MyNode implementation:
- (void)notifySelector:(NSNotification *)notify{
//Getted after some processing finished
id someValue = [notify.userInfo objectForKey:@"observedKey"];
[delegate updateItem:self WithValue:someValue ForKey:@"observedKey"];
}发布于 2012-08-23 17:14:18
别干那事。AppKit ( NSArrayController所属)通常不是线程安全的。相反,使用-performSelectorOnMainThread:...更新您的UI (包括NSArrayController)。总是在主线程上进行更新。
发布于 2016-05-02 15:22:53
约书亚和丹的解决方案是正确的。很有可能是在后台线程中对模型对象执行操作,该线程随后接触到数组控制器,从而触及表。
NSTableView本身并不是线程安全。简单地添加一个“Simply /endUpdate”对只会稍微避免竞争条件。然而,正如Fin所指出的,出于性能原因,做这对更新可能是件好事,但这对崩溃没有帮助。
要查找崩溃的来源,请在.mainThread上的代码中添加一些断言--特别是在您触摸数组控制器内容之前的任何位置。这将帮助您隔离问题。或者,子类NSTableView并将断言添加到某个键中,比如重写-numberOfRows (在更改时经常调用),以及调用超级。
-corbin
AppKit/NSTableView
发布于 2013-03-21 13:16:35
我解决了这个问题,在我的UI初始化一开始就添加了这个
[myTableView beginUpdates];然后在耐久的商店装完之后:
[myTableView endUpdates];这也使应用程序的启动变得更好,因为它不需要不断地从MOC重新加载所有的负载。
https://stackoverflow.com/questions/12094894
复制相似问题