3

I am trying to programmatically setup an NSArrayController to work with Core Data.

I know that my Core Data store has content since I can manually retrieve objects through the managed object context. I hooked up an NSArrayController to the same managed object context and then bound the value parameter of a NSTableColumn to the NSArrayController.

I asked the NSArrayController to fetch but it returns an empty array.

Any suggestions on what I might be doing wrong?

Interface

@interface MTTableViewController : NSObject <NSTableViewDelegate, NSTableViewDataSource>
{
    NSMutableArray *tableData;
    MTTableCell *tableCell;        

    IBOutlet NSTableColumn *tableColumn;        
    NSArrayController *dataController;
}

Implementation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        dataController = [[NSArrayController alloc] init];
        [dataController setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
        [dataController setEntityName:@"Track"];
        [dataController setAutomaticallyPreparesContent:YES];

        [dataController fetch:self];
        NSArray *content = [dataController arrangedObjects];        
        NSLog(@"Count :%i", (int)[content count]); //Outputs 0

        tableCell = [[MTTableCell alloc] initTextCell:@""];
        [tableColumn setDataCell:tableCell];
    }

    return self;
}
8
  • are sure your managed object context is initialised in the delegate by the time you assign it to your datacontroller init method. Commented Nov 22, 2011 at 17:19
  • I checked the call for the managed object context from the delegate and it is not nil. Any other suggestions? Commented Nov 22, 2011 at 20:43
  • try [dataController fetch:self]; after the setup. Shouldn't have to but... Commented Nov 22, 2011 at 20:51
  • I tried that but I still get zero objects. The SQLite persistence store is not empty. Ideas? Commented Nov 22, 2011 at 21:03
  • try using designated initialiser initWithContent: with nil Commented Nov 22, 2011 at 21:56

1 Answer 1

3

The fetch: doesn't wait for data to be loaded, instead it returns instantly.

This makes sense in bindings-enabled environment. You usually have a table view bound to an array controller, which updates whenever controller content changes.

In this case you could observe for changes in arrangedObjects of the controller:

[self.arrayController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:NULL];

And then:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  NSLog(@"Data: %@", self.arrayController.arrangedObjects);
}

Source: https://stackoverflow.com/a/13389460

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.