title: Core Data, on first run won't display data in uitableview, 2nd run ok
body: <p>I want to populate the UITableView with data from a plist file into core data</p>
  
  <p>It's so I easy can update the uitableview in later versions, then I only need to change in the plist life and the core data takes care of the rest. Please tell me if there is a better way...</p>
  
  <p>Anyway this I'm doing here ain't working properly, on the first run it fills the core data BUT the UITableView is empty, then if you close the app totally, also from multi-task-bar thingy and start it up again it now show the uitableview with data</p>
  
  <p>In my app delegate.h I have :</p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  
  if ( ![userDefaults valueForKey:@"version"] )
  {
      [self value1];
      [self value2];
      [self value3];
      [self value4];
      [self value5];
      [self value6];
  
      NSLog(@"running code");
      [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
  
      // Adding version number to NSUserDefaults for first version:
      [userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
  }
  
  if ([[NSUserDefaults standardUserDefaults] floatForKey:@"version"] == [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] )
  {
      /// Same Version so dont run the function
  }
  else
  {
      [Hovedmenu MR_truncateAll];
      [BarneDaab MR_truncateAll];
      [Graviditeten MR_truncateAll];
      [MineSygedomme MR_truncateAll];
      [Fortalt MR_truncateAll];
      [Familien MR_truncateAll];
  
      [self value2];
      [self value3];
      [self value4];
      [self value5];
      [self value6];
      [self value1];
  
      [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
      NSLog(@"running code agian");
  
      // Update version number to NSUserDefaults for other versions:
      [userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
  }
  }
  </code></pre>
  
  <p>and value1 2 3 4 5 6 are almost all the same but from different plists, here's and example</p>
  
  <pre><code>-(void)value1{
  NSManagedObjectModel *mom = [self managedObjectModel];
  NSDictionary *attrs = [[[mom entitiesByName] objectForKey:@"Hovedmenu"] attributesByName];
  NSArray *keyedValues = [[NSArray alloc] initWithContentsOfFile:
                          [[NSBundle mainBundle] pathForResource:@"hovedMenu" ofType:@"plist"]
                          ];
  
  for( NSDictionary *keyedValueDict in keyedValues ) {
      NSManagedObject *mObj = [NSEntityDescription insertNewObjectForEntityForName:@"Hovedmenu" inManagedObjectContext:[self managedObjectContext]];
      for (NSString *attribute in attrs) {
          id value = [keyedValueDict objectForKey:attribute];
          if (value == nil) {
              // Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues
              continue;
          }
          NSAttributeType attributeType = [[attrs objectForKey:attribute] attributeType];
          if ((attributeType == NSStringAttributeType) &amp;&amp; ([value isKindOfClass:[NSNumber class]])) {
              value = [value stringValue];
          } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) &amp;&amp; ([value isKindOfClass:[NSString class]])) {
              value = [NSNumber numberWithInteger:[value  integerValue]];
          } else if ((attributeType == NSFloatAttributeType) &amp;&amp; ([value isKindOfClass:[NSString class]])) {
              value = [NSNumber numberWithDouble:[value doubleValue]];
          }
          [mObj setValue:value forKey:attribute];
          NSLog(@"Value %@ for Key %@", value, attribute);
      }
  
  }
  NSError *error;
  [[self managedObjectContext] save:&amp;error];
  }
  </code></pre>
  
  <p>in my viewcontroller.h</p>
  
  <pre><code>-(void)viewWillAppear:(BOOL)animated{
  [self.mainTableView reloadData];
  [super viewWillAppear:animated];
  NSError *error = nil;
  if (![[self fetchedResultsController] performFetch:&amp;error]) {
      // Update to handle the error appropriately.
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      exit(-1);  // Fail
  }
  }
  - (NSFetchedResultsController *)fetchedResultsController {
  if (_fetchedResultsController != nil) {
      return _fetchedResultsController;
  }
  
      _fetchedResultsController = [Hovedmenu fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"numberRow" ascending:YES delegate:self];
  
  return _fetchedResultsController;
  </code></pre>
  
  <p>}</p>
  
  <p>I'm using MagicalRecord as well</p>