Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jun 30, 2009, 12:02 PM   #1
macrumors 6502a
Join Date: Jun 2005
Location: West-Europe
nstreecontroller rearrangeobjects bug?

Hi folks

I have my own NSTreeController and NSOutlineView subclass, with a coredata backend. I can drag and drop objects around and each object knows it position.

When I change something (add/remove/drag&drop) the position is correctly updated, but the view is not. [myTreeController rearrangeobjects] is called, but only the top group (with parent == nil) is automatically rearranged. All the subgroups are not.

My question is this. Did I overlook something, or is this a bug with NSTreeController? Does this problem sound familiar to you? NSTreeController has had a lot of bugs in the past, from what I can tell. Running 10.5.7 on intel.

thanks for reading
MrFusion is offline   0 Reply With Quote
Old Aug 14, 2009, 12:23 PM   #2
Thread Starter
macrumors 6502a
Join Date: Jun 2005
Location: West-Europe
I am trying to fix this bug once more, but with no luck so far. Maybe one of you sees the problem?

The bug is this. Adding a new node to a folder, when one of the nodes of that folder is selected, will add a new node to the tree with the correct value for the position. However, in the NSOutlineView the node is not correctly positioned in the tree.

-(id) addManagedObject:(NSString *) entityName {
	// First get selected object, then add new object. this returns the correct user selection!
	id parent = ([[self selectedObjects] count] > 0) ? [[self selectedObjects] objectAtIndex:0] : nil;
	int position = def_newNodePosition;
	if (parent && ![parent isFolder]) { //parent is a node itself, use its parent instead. rootnode = nil and a folder <if (![parent isFolder])> is true for rootnode
		position = [[parent valueForKey:@"position"] intValue] + def_incrementNodePosition;
		parent = [parent parent];
	return [self addManagedObject:entityName toParent:parent atPosition:position];

-(id) addManagedObject:(NSString *) entityName toParent:(id) parent atPosition:(int) position {
	id newNode = [NSEntityDescription insertNewObjectForEntityForName:entityName
 											   inManagedObjectContext:[self managedObjectContext]];  //Add a new node into the tree.
	[newNode setParent:parent];
	[newNode setValue:[NSNumber numberWithInt:position]
	[self resetPositionsFor:parent];
	[[self managedObjectContext] processPendingChanges]; //to speed up display of new node
	return newNode;

#pragma mark (re)order data
-(void) resetPositionsFor:(id)node {
	if (node) {
		[node resetPositions];
	} else {
		NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Group" 
															 inManagedObjectContext:[self managedObjectContext]];
		NSFetchRequest *request = [[NSFetchRequest alloc] init];
		[request setEntity:entityDescription];
		[request setIncludesSubentities:YES];
		NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"parent == nil"]];
		[request setPredicate:pred];
		NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"position"
		[request setSortDescriptors:[NSArray arrayWithObject:sort]];
		NSError *error = nil;
		NSArray *ordered = [[self managedObjectContext] executeFetchRequest:request
		[request release];
		//reset position of children
		for (int i = 0; i < [ordered count]; i++) {
			id item = [ordered objectAtIndex:i];
			[item setValue:[NSNumber numberWithInt:i*def_positioninterval]
			[item resetPositions]; //reset positions of childrenchildren of this child
		[sort release];

	//re-order and reload
	[self rearrangeObjects];
	[myOutlineView reloadData];
MrFusion is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Safari Weird Bug & Download folder Bug DJTaurus OS X Mavericks (10.9) 0 Feb 18, 2014 04:11 PM
iPhone: 7.1 b2 + b3 bug anyone? Raunien Jailbreaks and iOS Hacks 0 Jan 19, 2014 01:00 PM
New bug in 10.8.2 CylonGlitch OS X 10.8 Mountain Lion 4 Feb 22, 2013 08:07 PM
iPhone: IntelliscreenX Bug or Messages+ bug?ip5 x-evil-x Jailbreaks and iOS Hacks 7 Feb 11, 2013 11:30 PM
Bind NSOutlineView & NSTextView to NSTreeController? ArtOfWarfare Mac Programming 3 Dec 9, 2012 11:25 AM

Forum Jump

All times are GMT -5. The time now is 10:55 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC