Search code examples
macoscocoa-bindingsnsarraycontroller

NSArrayController+cocoa binding + core data: get the selected row at application Launching


I'm using cocoa-binding, NSArrayController and core data. Just after Launching, the application have to get the first item, i try this in applicationDidFinishLaunching:

1. Entity *ent = arrayManager.arrangedObjects[0];

2. Entity *ent = arrayManager.selectedObjects[0];

---> error saying that the array is empty. why ?

other thing that i don't understand is that when the application appear, in the tableView there's a selected row, but when i log this :

NSLog(@"selected row in applicationDidFinishLaunching = %li",self.TableViewController.tableView.selectedRow);

log: -1 --> no row selected ! why ?


Solution

  • You have to do a fetch on the ArrayController in applicationDidFinishLaunching: in order to see your Entities there. Try doing this before accessing anything from the ArrayController:

     - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
         NSError *error;
         [arrayManager fetchWithRequest:nil merge:NO error:&error];
         Entity *ent = arrayManager.arrangedObjects[0];
     }