开发者

NSTreeController + Core Data: STOP KVO?

Context

I have a very normal setup in my app: an NSOutlineView (backed by an NSTreeController bound to Core Data) fills an NSTableView (backed by an NSArrayController bound to core data) based on what the user selects in the outlineView. Both controllers (and their views) are bound in the normal way ---> contentSet binding on NSArrayController points to an NSSet property of the object represented by the selected row in the outlineView.

So, suppose the user selects "Folder X" in the outlineView. There are 1,200 items in "folder X", each of which is backed by a Core Data entity and each of which is a row in the ta开发者_如何学编程bleView.

The Problem

My app needs to change a property on each of those 1,200 entities, so I have a loop that does just that. But, EVERY time I access an entity and set a new property, the NSTreeController and NSArrayController fire off a KVO notification with the changed entity as the object of the notification. This is KILLING performance.

If nothing is selected in the outlineView, the loop completes more or less instantly. If "folder X" is selected, those KVO notifications slow the process down to ~8 seconds.

What I need

A way to STOP the controllers from firing KVO notifications, or a way to work around the issue so that updating the entities is not slower if they're selected in an NSController.


You can use the -setPrimitiveValue:forKey: method of NSManagedObject. This will allow you to set the value of a property without triggering KVO observers.

Note that in general, you should use the dynamically-generated accessors rather than using this method directly (for example, setPrimitiveName: instead of setPrimitiveValue:newName forKey:@"name").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜