title: Preloaded database with core data
body: <p>I am trying to use a preloaded Sqlite database for my universal app, which uses core data. It use to work with iOS 4.3, but not with iOS 5.0+ and I'm using Xcode 4.3. </p>
  
  <p>I've looked at the linked below, made changes, changed back, but to no avail and now the preloading does not work at all.</p>
  
  <p><a href="https://stackoverflow.com/questions/8041278/prepopulate-core-data-in-ios-5">Prepopulate Core Data in iOS 5</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4964901/coredata-problem-nsurl-may-not-respond-to-stringbyappendingpathcomponent">coredata problem nsurl may not respond to stringByAppendingPathComponent</a></p>
  
  <p>What am I doing wrong, I have listed my code below, any help would be greatly appreciated. Thank you for your time.</p>
  
  <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
  {
  
      if (persistentStoreCoordinator != nil) 
      {
          return persistentStoreCoordinator;
      }
  
      NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"SAP_ECC_DB.sqlite"];
      //NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SAP_ECC_DB.sqlite"];
  
       //Set up the store.
       //For the sake of illustration, provide a pre-populated default store.
      NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
      // If the expected store doesn't exist, copy the default store.
      if (![fileManager fileExistsAtPath:storePath]) 
      {
          NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"SAP_ECC_DB" ofType:@"sqlite"];
          if (defaultStorePath) 
          {
              [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
          }
      }
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];    
      persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
  
      NSError *error;
      if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&amp;error]) {
          // Update to handle the error appropriately.
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          exit(-1);  // Fail
      } 
  
  
  
  - (NSString *)applicationDocumentsDirectory
  {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil;
      return basePath;
      //return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  }
  </code></pre>