title: Coredata - array of fetchedobjects empty when called from other class
body: <p>From the - viewDidLoad () of a UIViewController class (called StageDetailViewController) I am calling an instance method on another class (called ContentController).</p>
  
  <pre><code> - (void)viewDidLoad {
          [super viewDidLoad];
          ContentController * instance = nil;
          instance = [[ContentController alloc] init];
          [instance fetchSectorList];
          [instance release];
  
         ....other stuff ....
  
  }
  </code></pre>
  
  <p>The called method (fetchSectorList) looks like this:</p>
  
  <pre><code>- (void)fetchSectorList {
  
       NSLog(@"fetchSectorList method called");
  
       NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription 
                                 entityForName:@"Sectors" inManagedObjectContext:self.managedObjectContext];
       [fetchRequest setEntity:entity];
        NSError *error;
        NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error];
  
        for (NSManagedObject *info in fetchedObjects) {
             NSLog(@"aircraft in fetchsectorlist: %@", [info valueForKey:@"aircraft"]);
        }
  
  [fetchRequest release]; 
  
  }
  </code></pre>
  
  <p>The method is of type void simply because at this stage all I want to do is call it and ensure that it has some valid contents.</p>
  
  <p>Here is the problem: When I call (void)fetchSectorList from StageDetailViewController, the NSLog file confirms that it has been called:</p>
  
  <p>"fetchSectorList method called"</p>
  
  <p>... but the for loop does not return anything!</p>
  
  <p>HOWEVER, when I call the (void)fetchSectorList internally from within the ContentController class, a list of "aircraft" are returned by the for loop.</p>
  
  <pre><code>- (NSFetchedResultsController *)stageResultsController {
      [self fetchSectorList];
      return [self resultsControllerForEntity:@"Stage"];
  }
  </code></pre>
  
  <p>Why then can I not call this same method from another class? Please help. All contributions very welcome.</p>
  
  <p>Notes: </p>
  
  <p>1) I declare NSManagedObjectModel, NSManagedObjectContext and NSPersistentStoreCoordinatorin ContentController.h
  2) I inherited the skeleton for the core data code from an example on the internet. It defines a class method that is used by another class in the example to init the class.</p>
  
  <pre><code>+ (ContentController *)sharedInstance {
      static ContentController * instance = nil;
      if (!instance) {
          instance = [[ContentController alloc] init];
  </code></pre>
  
  <p>I however am choosing to initialise from the initialiser code shown above in the viewdidload () of my calling class (StageDetailViewController). Could this be part of the problem?</p>
  
  <p>Thanks for your patience. I'd really appreciate some input here. I'd be happy to provide clarification of any of the above.</p>
  
  <p><strong>Solution!!!</strong></p>
  
  <p>Guys, thanks for all the advice. Following Marcus's comments I delved a little deeper into Singletons and the call that gives the desired result from StageDetailViewController in -viewdidload() is simply:</p>
  
  <p>[[ContentController sharedInstance] fetchSectorList];</p>
  
  <p>The Singleton in my app:</p>
  
  <ul>
  <li>(ContentController *)sharedInstance</li>
  </ul>
  
  <p>... will always have been initialised by the time the StageDetailViewController is initialised, and so being a singleton, it only needs to be initialised once in the runtime of the app. So a simple call to the fetchSectorList method works.</p>
  
  <p>Marcus, sorry to associate your name with Singletons ... I vow to widen my armoury at a later stage.</p>