NSArrayController rearrangeObjects error

前提是你 提交于 2019-12-08 06:46:12

问题


I have troubles with NSArrayController rearrangeObjects function - this function called from some background treads and sometimes App crashes with error: 'endUpdates called without a beginUpdates'.

How can i detect if arrayController is rearranging objects in current moment and add next rearrange to some like queue or cancel current rearranging and run new?

May be there is another solution for this problem?

Edit code added:

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"];
}

回答1:


Don't do that. AppKit (to which NSArrayController belongs) is not generally thread safe. Instead, use -performSelectorOnMainThread:... to update your UI (including NSArrayController). ALWAYS do updating on the main thread.




回答2:


Joshua and Dan's solution is correct. It is highly likely that you are performing operations on your model object in a background thread, which then touches the array controller, and hence touches the table.

NSTableView itself is not threadsafe. Simply adding in a "beginUpdates/endUpdates" pair will just avoid the race condition for a bit. However, like Fin noted, it might be good to do the pair of updates for performance reasons, but it won't help with the crash.

To find the sources of the crash, add some assertions in your code on ![NSThread currentThread].mainThread -- particularly any places before you touch the array controller's content. This will help you isolate the problem. Or, subclass NSTableView and add the assertion in somewhere key, like overriding -numberOfRows (which is called frequently on changes), and calling super.

-corbin
AppKit/NSTableView




回答3:


I solved this by adding this at the very start of my UI initialisation

[myTableView beginUpdates];

and then at the end after the persistant store has been loaded fully:

[myTableView endUpdates];

This also make the app startup a lot better since it does not constantly have to reload all loads from the MOC.



来源:https://stackoverflow.com/questions/12094894/nsarraycontroller-rearrangeobjects-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!