How do you populate a NSArrayController with CoreData rows programmatically?

Posted by Andrew McCloud on Stack Overflow See other posts from Stack Overflow or by Andrew McCloud
Published on 2011-02-15T10:10:10Z Indexed on 2011/02/20 7:25 UTC
Read the original article Hit count: 227

After several hours/days of searching and diving into example projects i've concluded that I need to just ask. If I bind the assetsView (IKImageBrowserView) directly to an IB instance of NSArrayController everything works just fine.

- (void) awakeFromNib
{
    library = [[NSArrayController alloc] init];
    [library setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
    [library setEntityName:@"Asset"];       

    NSLog(@"%@", [library arrangedObjects]);
    NSLog(@"%@", [library content]);

    [assetsView setDataSource:library];
    [assetsView reloadData];
}

Both NSLogs are empty. I know i'm missing something... I just don't know what. The goal is to eventually allow multiple instances of this view's "library" filtered programmatically with a predicate. For now i'm just trying to have it display all of the rows for the "Asset" entity.

Addition: If I create the NSArrayController in IB and then try to log [library arrangedObjects] or manually set the data source for assetsView I get the same empty results. Like I said earlier, if I bind library.arrangedObjects to assetsView.content (IKImageBrowserView) in IB - with same managed object context and same entity name set by IB - everything works as expected.

- (void) awakeFromNib
{
//  library = [[NSArrayController alloc] init];
//  [library setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
//  [library setEntityName:@"Asset"];       

    NSLog(@"%@", [library arrangedObjects]);
    NSLog(@"%@", [library content]);

    [assetsView setDataSource:library];
    [assetsView reloadData];
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa