문제

My Model looks like this:

model

In my test project, I have the following two methods:

- (void) addChildWithName:(NSString*)name toParent:(Item*)parent
{
    static NSUInteger count = 1;

    Item*   childItem;


    childItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
    [childItem setName:[NSString stringWithFormat:@"%@ %lu", name, count]];
    [childItem setParent:parent];

    count++;
}



- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.


    Item*   rootItem;

    rootItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
    [rootItem setName:@"rootItem"];

    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
}

This results in an outline view which looks like:

result

For my tree controller object in the xib, I have set the Children Key Path to 'children'. The managed object context (moc) is bound to the file owners moc. The Value of table column of my outline view is bound to the NSTreeController's arrangedObjects with the model key path of 'name'.

As you can see, I am getting duplicate entries for the child items when they should only be appearing under the root item.

What am I doing wrong?

The sample project is located at sample project link.

Thank you.

도움이 되었습니까?

해결책

Your array controller needs a "Fetch Predicate" to only select Item objects when the parent attribute is nil using parent == nil.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top