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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With