title: A Core Data Fetch crash
body: <p>I'm sharing a Core Data Controller class between versions of my apps.</p>
  
  <p>The code works fine for an iPhone version but stops short on the iPad version.</p>
  
  <p>Here's the code called from the RootVC:</p>
  
  <pre><code>    #pragma mark - NSFetchedResultsController
  
      - (NSFetchedResultsController *)fetchedResultsController {
           NSLog(@"%s", __FUNCTION__);
          if (_fetchedResultsController != nil) {
              return _fetchedResultsController;
          }
  
  
           // Create the fetch request for the entity   
          NSLog(@"1");
          NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
  
   // Set up the fetched results controller
  
          NSLog(@"2");
          NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
          NSLog(@"3");
  
          [fetchRequest setEntity:entity];
         NSLog(@"4");
          // Set the batch size to a suitable number
          [fetchRequest setFetchBatchSize:20];
  
          // Edit the sort key as appropriate.
          NSSortDescriptor *titleSortDesc = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
          NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSortDesc, nil];
  
          [fetchRequest setSortDescriptors:sortDescriptors];
  
          // Edit the section name key path and cache name if appropriate,
          // nil for section name key path means "no sections"
          NSFetchedResultsController *aFetchedResultsController =
              [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                  managedObjectContext:self.managedObjectContext
                                                    sectionNameKeyPath:nil
                                                             cacheName:nil];
  
          aFetchedResultsController.delegate = self;
          self.fetchedResultsController = aFetchedResultsController;
  
          return _fetchedResultsController;
      }    
  </code></pre>
  
  <p>Log entry 1 shows. Log entry 2 doesn't show. I get this crash instead:</p>
  
  <pre><code>2012-11-19 08:37:26.402  I'm an iPad
  2012-11-19 08:37:26.400  -[CoreDataController asyncLoadPersistentStores]
  2012-11-19 08:37:26.403  -[CoreDataController loadiCloudStore:]
  2012-11-19 08:37:26.404  moc is &lt;NSManagedObjectContext: 0x1e03e4f0&gt;
  2012-11-19 08:37:26.421  -[CoreDataController iCloudStoreURL]
  2012-11-19 08:37:26.454  -[CoreDataController folderForUbiquityToken:]
  2012-11-19 08:37:26.460  -[RootViewController viewDidLoad]
  2012-11-19 08:37:26.469   RVC moc is (null)
  2012-11-19 08:37:26.470  -[RootViewController fetchedResultsController]
  2012-11-19 08:37:26.472  1
  2012-11-19 08:37:26.474  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
  *** First throw call stack:
  </code></pre>
  
  <p>I'm passing the managed object context from the App Delegate to the RootVC like this:</p>
  
  <pre><code>rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
  </code></pre>
  
  <p>I could use some help figuring this out. I dont get whats different between the way it works for iPhone and OSX but not for iPad.. Same share data controller.</p>
  
  <p>UPDATE:</p>
  
  <p>These are the results from logs suggested in a comment by Phillip, below...</p>
  
  <pre><code>RVC is &lt;RootViewController: 0x1f88d1a0&gt;
  -[CoreDataController loadiCloudStore:]
  MOC is &lt;NSManagedObjectContext: 0x1eda3630&gt;
  -[CoreDataController iCloudStoreURL]
  -[RootViewController viewDidLoad]
  -[CoreDataController folderForUbiquityToken:]
  -[RootViewController fetchedResultsController]
  1
  SELF is &lt;RootViewController: 0x1f8859d0&gt;
  </code></pre>