title: CoreData and NSManagedObjectContext
body: <p>I've got this function that will move objects from a "fallbackstore" to the iCloud store, fallbackstore is the store where objects where saved when iCloud is unavailable.</p>
  
  <p>Bottom line, I need to move objects (with their relationship) from one context to another one.</p>
  
  <p>But I'm getting this error:</p>
  
  <p><strong>* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Context already has a coordinator;  cannot replace.'
  *</strong> First throw call stack:</p>
  
  <p>on:</p>
  
  <p>foodSuccess = [moc save:&amp;localError];</p>
  
  <p>Anybody knows where I'm getting wrong (and why)? Thanks in advance!</p>
  
  <pre><code>    - (BOOL)seedStore:(NSPersistentStore *)store withPersistentStoreAtURL:(NSURL *)seedStoreURL error:(NSError * __autoreleasing *)error {
      BOOL success = YES;
  
      NSLog(@"%s", __func__);
  
      BOOL foodSuccess = YES;
      BOOL sportSuccess = YES;
      BOOL dailySuccess = YES;
      BOOL activitySuccess = YES;
      BOOL reportSuccess = YES;
      BOOL userSuccess = YES;
      BOOL ingredientSuccess = YES;
  
      NSUInteger batchSize = 5000;
  
      NSError *localError = nil;
  
      NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
      NSPersistentStoreCoordinator *seedPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
      NSDictionary *seedStoreOptions = @{ NSReadOnlyPersistentStoreOption : [NSNumber numberWithBool:YES] };
      NSPersistentStore *seedStore = [seedPSC addPersistentStoreWithType:NSSQLiteStoreType
                                                           configuration:nil
                                                                     URL:seedStoreURL
                                                                 options:seedStoreOptions
                                                                   error:&amp;localError];
      if (seedStore) {
          NSManagedObjectContext *seedMOC = [[NSManagedObjectContext alloc] init];
          [seedMOC setPersistentStoreCoordinator:seedPSC];
  
          // Food
          NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Food"];
          fr.relationshipKeyPathsForPrefetching = @[@"daily", @"ingredient", @"ingredients"];
          [fr setFetchBatchSize:batchSize];
  
          NSArray *foods = [seedMOC executeFetchRequest:fr error:&amp;localError];
          NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          [moc setPersistentStoreCoordinator:_psc];
          NSUInteger i = 1;
          for (Food *f in foods) {
              [self addFood:f toStore:store withContext:moc];
              if (0 == (i % batchSize)) {
                  foodSuccess = [moc save:&amp;localError];
                  if (foodSuccess) {
                      [moc reset];
                  } else {
                      NSLog(@"Error saving during seed (Food): %@", localError);
                      break;
                  }
              }
  
              i++;
          }
  
          if ([moc hasChanges]) {
              foodSuccess = [moc save:&amp;localError];
              [moc reset];
          }
  
          // Sport
          NSFetchRequest *fetchSports = [NSFetchRequest fetchRequestWithEntityName:@"Sport"];
          fetchSports.relationshipKeyPathsForPrefetching = @[@"activity"];
          fetchSports.fetchBatchSize = batchSize;
  
          NSArray *sports = [seedMOC executeFetchRequest:fetchSports error:&amp;localError];
          NSManagedObjectContext *sportContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          sportContext.persistentStoreCoordinator = _psc;
          NSUInteger iSports = 1;
          for (Sport *s in sports) {
              [self addSport:s toStore:store withContext:sportContext];
              if (0 == (iSports % batchSize)) {
                  sportSuccess = [moc save:&amp;localError];
                  if (sportSuccess) {
                      [moc reset];
                  } else {
                      NSLog(@"Error saving during seed (Sport): %@", localError);
                      break;
                  }
              }
  
              iSports++;
          }
  
          if ([sportContext hasChanges]) {
              sportSuccess = [sportContext save:&amp;localError];
              [sportContext reset];
          }
  
          // Daily
          NSFetchRequest *fetchDailies = [NSFetchRequest fetchRequestWithEntityName:@"Daily"];
          fetchDailies.relationshipKeyPathsForPrefetching = @[@"food", @"user"];
          fetchDailies.fetchBatchSize = batchSize;
  
          NSArray *dailies = [seedMOC executeFetchRequest:fetchDailies error:&amp;localError];
          NSManagedObjectContext *dailiesContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          dailiesContext.persistentStoreCoordinator = _psc;
          NSUInteger iDailies = 1;
          for(Daily *d in dailies) {
              [self addDaily:d toStore:store withContext:dailiesContext];
              if(0 == (iDailies % batchSize)) {
                  dailySuccess = [dailiesContext save:&amp;localError];
                  if (dailySuccess) {
                      [dailiesContext reset];
                  }
                  else {
                      NSLog(@"Error saving during seed (Daily): %@", localError);
                      break;
                  }
              }
  
              iDailies++;
          }
  
          if ([dailiesContext hasChanges]) {
              dailySuccess = [dailiesContext save:&amp;localError];
              [dailiesContext reset];
          }
  
          // Ingredient
          NSFetchRequest *fetchIngredients = [NSFetchRequest fetchRequestWithEntityName:@"Ingredient"];
          fetchIngredients.relationshipKeyPathsForPrefetching = @[@"food", @"foods"];
          fetchIngredients.fetchBatchSize = batchSize;
  
          NSArray *ingredients = [seedMOC executeFetchRequest:fetchIngredients error:&amp;localError];
          NSManagedObjectContext *ingredientsContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          ingredientsContext.persistentStoreCoordinator = _psc;
          NSUInteger iIngredients = 1;
          for(Ingredient *i in ingredients) {
              [self addIngredient:i toStore:store withContext:ingredientsContext];
              if(0 == (iIngredients % batchSize)) {
                  ingredientSuccess = [ingredientsContext save:&amp;localError];
                  if (ingredientSuccess) {
                      [ingredientsContext reset];
                  }
                  else {
                      NSLog(@"Error saving during seed (Ingredient): %@", localError);
                      break;
                  }
              }
  
              iIngredients++;
          }
  
          if ([ingredientsContext hasChanges]) {
              ingredientSuccess = [ingredientsContext save:&amp;localError];
              [ingredientsContext reset];
          }
  
          // Activity
          NSFetchRequest *fetchActivities = [NSFetchRequest fetchRequestWithEntityName:@"Activity"];
          fetchActivities.relationshipKeyPathsForPrefetching = @[@"sport", @"user"];
          fetchActivities.fetchBatchSize = batchSize;
  
          NSArray *activities = [seedMOC executeFetchRequest:fetchActivities error:&amp;localError];
          NSManagedObjectContext *activitiesContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          dailiesContext.persistentStoreCoordinator = _psc;
          NSUInteger iActivities = 1;
          for(Activity *a in activities) {
              [self addActivity:a toStore:store withContext:activitiesContext];
              if(0 == (iActivities % batchSize)) {
                  activitySuccess = [activitiesContext save:&amp;localError];
                  if (activitySuccess) {
                      [activitiesContext reset];
                  }
                  else {
                      NSLog(@"Error saving during seed (Activity): %@", localError);
                      break;
                  }
              }
  
              iActivities++;
          }
  
          if ([activitiesContext hasChanges]) {
              activitySuccess = [activitiesContext save:&amp;localError];
              [activitiesContext reset];
          }
  
          // Report
          NSFetchRequest *fetchReports = [NSFetchRequest fetchRequestWithEntityName:@"Report"];
          fetchReports.relationshipKeyPathsForPrefetching = @[@"user"];
          fetchReports.fetchBatchSize = batchSize;
  
          NSArray *reports = [seedMOC executeFetchRequest:fetchReports error:&amp;localError];
          NSManagedObjectContext *reportsContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          reportsContext.persistentStoreCoordinator = _psc;
          NSUInteger iReports = 1;
          for(Report *r in reports) {
              [self addReport:r toStore:store withContext:reportsContext];
              if(0 == (iReports % batchSize)) {
                  reportSuccess = [reportsContext save:&amp;localError];
                  if (reportSuccess) {
                      [reportsContext reset];
                  }
                  else {
                      NSLog(@"Error saving during seed (Report): %@", localError);
                      break;
                  }
              }
  
              iReports++;
          }
  
          if ([reportsContext hasChanges]) {
              reportSuccess = [reportsContext save:&amp;localError];
              [reportsContext reset];
          }
  
          // User
          NSFetchRequest *fetchUsers = [NSFetchRequest fetchRequestWithEntityName:@"User"];
          fetchUsers.relationshipKeyPathsForPrefetching = @[@"activities", @"dailies", @"reports"];
          fetchUsers.fetchBatchSize = batchSize;
  
          NSArray *users = [seedMOC executeFetchRequest:fetchUsers error:&amp;localError];
          NSManagedObjectContext *usersContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
          usersContext.persistentStoreCoordinator = _psc;
          NSUInteger iUsers = 1;
          for(User *u in users) {
              [self addUser:u toStore:store withContext:usersContext];
              if(0 == (iUsers % batchSize)) {
                  userSuccess = [usersContext save:&amp;localError];
                  if (userSuccess) {
                      [usersContext reset];
                  }
                  else {
                      NSLog(@"Error saving during seed (User): %@", localError);
                      break;
                  }
              }
  
              iUsers++;
          }
  
          if ([usersContext hasChanges]) {
              userSuccess = [usersContext save:&amp;localError];
              [usersContext reset];
          }
  
          // Result
  
          success = foodSuccess &amp;&amp; sportSuccess &amp;&amp; dailySuccess &amp;&amp; ingredientSuccess &amp;&amp; activitySuccess &amp;&amp; reportSuccess &amp;&amp; userSuccess;
  
      } else {
          success = NO;
          NSLog(@"Error adding seed store: %@", localError);
      }
  
      if (NO == success) {
          if (localError  &amp;&amp; (error != NULL)) {
              *error = localError;
          }
      }
  
      return success;
  }
  
  
      - (void)addFood:(Food *)food toStore:(NSPersistentStore *)store withContext:(NSManagedObjectContext *)moc
  {
      NSEntityDescription *entity = [food entity];
      Food *newFood = [[Food alloc] initWithEntity:entity insertIntoManagedObjectContext:moc];
  
      newFood.alcol = food.alcol;
      newFood.amid = food.amid;
      newFood.bookmark = food.bookmark;
      newFood.calcium = food.calcium;
      newFood.cho = food.cho;
      newFood.cholesterol = food.cholesterol;
      newFood.complex = food.complex;
      newFood.copper = food.copper;
      newFood.dirty = food.dirty;
      newFood.edible = food.edible;
      newFood.fat = food.fat;
      newFood.fatMono = food.fatMono;
      newFood.fatPoli = food.fatPoli;
      newFood.fatSat = food.fatSat;
      newFood.fibre = food.fibre;
      newFood.iron = food.iron;
      newFood.kcal = food.kcal;
      newFood.lookback = food.lookback;
      newFood.magnesium = food.magnesium;
      newFood.name = food.name;
      newFood.note = food.note;
      newFood.phosphorus = food.phosphorus;
      newFood.potassium = food.potassium;
      newFood.pro = food.pro;
      newFood.recipe = food.recipe;
      newFood.recordUUID = (food.recordUUID == nil) ? [[[NSUUID alloc] init] UUIDString] : food.recordUUID;
      newFood.serving = food.serving;
      newFood.servingDesc = food.servingDesc;
      newFood.sodium = food.sodium;
      newFood.userAdd = food.userAdd;
      newFood.vitA = food.vitA;
      newFood.vitC = food.vitC;
      newFood.vitE = food.vitE;
      newFood.water = food.water;
      newFood.zinc = food.zinc;
      newFood.dosePeople = food.dosePeople;
      newFood.daily = food.daily;
  
      for(Ingredient *i in food.ingredients) {
          NSEntityDescription *entityIngredient = [i entity];
          Ingredient *newIngredient = [[Ingredient alloc] initWithEntity:entityIngredient insertIntoManagedObjectContext:moc];
          [newFood addIngredientsObject:newIngredient];
      }
  
      NSMutableSet *ing = [food mutableSetValueForKey:@"ingredient"];
      for(Ingredient *i in ing) {
          NSLog(@"%s Name: %@", __func__, i.food.name);
  
          NSEntityDescription *entityIngredient = [i entity];
          Ingredient *newIngredient = [[Ingredient alloc] initWithEntity:entityIngredient insertIntoManagedObjectContext:moc];
          [newFood addIngredientsObject:newIngredient];
      }
  
  
      [moc assignObject:newFood toPersistentStore:store];
  }
  </code></pre>