views:

226

answers:

1

I have an NSArray of strings and I want to add a certain amount of rows to the outline view depending on how many strings are in the array, each with the title of the String that was added.

I think it would involve looping through the array like this.

for(NSString *title in array) {
    JGManagedObject *theParent = 
        [NSEntityDescription insertNewObjectForEntityForName:@"projects"
                                      inManagedObjectContext:managedObjectContext];
    [theParent setValue:nil forKey:@"parent"];
    [theParent setValue:@"Project" forKey:@"name"];
    [theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"];

}
+1  A: 

Don't bother. The tree controller sits between the model (The Core data store that Spark is using) and the view (your source view). Instead of adding from the array to the tree controller, you should be adding from the array to the data store.

The tree controller will pick up the changes in the model and show the changes in the view.

Edit:

(Bear in mind it is hard to debug from a distance.)

With Garbage Collection, if you don't hold on to your objects, they are liable to be cleaned up from underneath you.

Try this and see what happens:

for(NSString *title in array) {
    NSManagedObjectContext *moc = [self managedObjectContext];
    JGManagedObject *theParent = 
        [NSEntityDescription insertNewObjectForEntityForName:@"projects"
                                      inManagedObjectContext:moc];
    [theParent setValue:nil forKey:@"parent"];
    // This is where you add the title from the string array
    [theParent setValue:title forKey:@"name"]; 
    [theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"];

}
Abizern
Hmm. Ok. How would I do that? I'm used to just adding to the Tree Controller.
Joshua
Create model objects from each string in the array and add it to the managed object context.
Abizern
Like … http://snapplr.com/xc5y
Joshua
@Joshua - that isn't a bad start! Have you tried actually running that and seeing what happens? I've edited your question to make it better. Add the results you see and the problems you have.
Abizern
Thanks! What happens is that only one item is ever added and it throws a load of errors at me in the Debugger. http://snapplr.com/y5mx
Joshua
And the name I set never actually shows as that when it is added, it just shows the default.
Joshua
Okay, set a breakpoint at each line and step through them to see which line is causing the error.
Abizern
Okay, I've found the line which is causing the error it's `[theParent setValue:@"Project" forKey:@"name"]; `
Joshua
No, that line is not causing that error. You under-retained your managed object context.
Peter Hosey
Oh well, that's odd, it showed the error came up on that line. So how do i retain my managed object context?
Joshua
Thanks that works!
Joshua