Kirill
Kirill

Reputation: 1

NSTreeController's selectionIndexPaths updating after mouseDown is often interrupted

Problem Background:

I have a NSOutlineView with every tableColumn binded programmatically to the NSTreeController's arrangedObjects so there is no need to bind selectionIndexPaths. The source of NSTreeController's arrangedObjects is a mutableArray. I'm adding all nodes to the NSTreeController dynamically by performing - (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath; on main thread. I have overridden NSOutlineView's mouseDown event in the way like: - (void)mouseDown:(NSEvent *)event { /*...myMethods...*/ [super mouseDown:event]; }

The Problem:

When the nodes are being added very fast and I perform the mouseDown event on the outlineView, then very often the next situation takes place:

the thread that adds nodes to TreeController interrupts the sequence (I guess) called by mouseDown event so insertObject: atArrangedObjectIndexPath: is called before then setSelectionIndexPaths:. That's why the new selection in outlineView disappears and treeController still has the old version of selectedIndexPaths.

I've tried one partial solution: blocked my insertObject: method (with @synthesized(outlineView)) so that it couldn't change the entire of outlineView, but it often rises in thread conflict and app freezes.

Are there any ideas how to solve the problem with disappearing selections?

Upvotes: 0

Views: 340

Answers (1)

SteAp
SteAp

Reputation: 11999

Synchronize the statements within the datasource update thread with the main-event-loop GCD thread. This way, mouseDown event handling and datasource updating get fully serialized:

dispatch_async(dispatch_get_main_queue(), ^{
  // data source update goes here
});

Upvotes: 0

Related Questions