Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:26:00+00:00 2026-06-07T01:26:00+00:00

I’ve got an iPhone app that uses many-to-many relationships to link tags and notes

  • 0

I’ve got an iPhone app that uses many-to-many relationships to link tags and notes together. I’m currently using Core Data’s “Relationships” feature to accomplish this, but would like to migrate to using a join table instead.

Here’s my challenge: I’d like to migrate from the old model to the join-table model, and I need to figure out how to perform that data migration.

Are there any good examples of how to do this?

Update: I’m clarifying my question here to help out with what’s going on here: I want to try using Simperium to support our app, but Simperium doesn’t support many-to-many relationships (!).

As an example of what I’m trying to do, let’s use the iPhoneCoreDataRecipes app as an example.

Here’s what my Core Data scheme currently resembles:
enter image description here

…and here’s what I’m transitioning to:
enter image description here

How do I get from one to the other, and bring the data with me?

Apple’s documentation for Core Data Migration is notoriously sparse, and I don’t see any useful walkthroughs for using an NSEntityMapping or NSMigrationManager subclass to get the job done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:26:02+00:00Added an answer on June 7, 2026 at 1:26 am

    Here is the basic process:

    1. Create a versioned copy of the Data Model. (Select the Model, then Editor->Add Model Version)

    2. Make your changes to the new copy of the data model

    3. Mark the copy of the new data model as the current version. (Click the top level xcdatamodel item, then in the file inspector set the “Current” entry under “Versioned Data Model” section to the new data model you created in step 1.

    4. Update your model objects to add the RecipeIngredient entity. Also replace the ingredients and recipes relationships on Recipe and Ingredient entities with new relationships you created in step 2 to the RecipeIngredient Entity. (Both entities get this relation added. I called mine recipeIngredients) Obviously wherever you create the relation from ingredient to recipe in the old code, you’ll now need to create a RecipeIngredient object.. but that’s beyond the scope of this answer.

    5. Add a new Mapping between the models (File->New File…->(Core Data section)->Mapping Model. This will auto-generate several mappings for you. RecipeToRecipe, IngredientToIngredient and RecipeIngredient.

    6. Delete the RecipeIngredient Mapping. Also delete the recipeIngredient relation mappings it gives you for RecipeToRecipe and IngredientToRecipe (or whatever you called them in step 2).

    7. Drag the RecipeToRecipe Mapping to be last in the list of Mapping Rules. (This is important so that we’re sure the Ingredients are migrated before the Recipes so that we can link them up when we’re migrating recipes.) The migration will go in order of the migration rule list.

    8. Set a Custom Policy for the RecipeToRecipe mapping “DDCDRecipeMigrationPolicy” (This will override the automatic migration of the Recipes objects and give us a hook where we can perform the mapping logic.

    9. Create DDCDRecipeMigrationPolicy by subclassing NSEntityMigrationPolicy for Recipes to override createDestinationInstancesForSourceInstance (See Code Below). This will be called once for Each Recipe, which will let us create the Recipe object, and also the related RecipeIngredient objects which will link it to Ingredient. We’ll just let Ingredient be auto migrated by the mapping rule that Xcode auto create for us in step 5.

    10. Wherever you create your persistent object store (probably AppDelegate), ensure you set the user dictionary to auto-migrate the data model:

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
          configuration:nil 
          URL:storeURL 
          options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  nil] 
          error:&error])
    {
    }
    

    Subclass NSEntityMigrationPolicy for Recipes

    #import <CoreData/CoreData.h>
    @interface DDCDRecipeMigrationPolicy : NSEntityMigrationPolicy
    @end
    

    *Override createDestinationInstancesForSourceInstance in DDCDRecipeMigrationPolicy.m *

    - (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
    {
    
        NSLog(@"createDestinationInstancesForSourceInstance : %@", sInstance.entity.name);
    
       //We have to create the recipe since we overrode this method. 
       //It's called once for each Recipe.  
        NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:[manager destinationContext]];
        [newRecipe setValue:[sInstance valueForKey:@"name"] forKey:@"name"];
        [newRecipe setValue:[sInstance valueForKey:@"overview"] forKey:@"overview"];
        [newRecipe setValue:[sInstance valueForKey:@"instructions"] forKey:@"instructions"];
    
        for (NSManagedObject *oldIngredient in (NSSet *) [sInstance valueForKey:@"ingredients"])
        {
            NSFetchRequest *fetchByIngredientName = [NSFetchRequest fetchRequestWithEntityName:@"Ingredient"];
            fetchByIngredientName.predicate = [NSPredicate predicateWithFormat:@"name = %@",[oldIngredient valueForKey:@"name"]];
    
            //Find the Ingredient in the new Datamodel.  NOTE!!!  This only works if this is the second entity migrated.
             NSArray *newIngredientArray = [[manager destinationContext] executeFetchRequest:fetchByIngredientName error:error];
    
            if (newIngredientArray.count == 1)
            {
                 //Create an intersection record. 
                NSManagedObject *newIngredient = [newIngredientArray objectAtIndex:0];
                NSManagedObject *newRecipeIngredient = [NSEntityDescription insertNewObjectForEntityForName:@"RecipeIngredient" inManagedObjectContext:[manager destinationContext]];
                [newRecipeIngredient setValue:newIngredient forKey:@"ingredient"];
                [newRecipeIngredient setValue:newRecipe forKey:@"recipe"];
                 NSLog(@"Adding migrated Ingredient : %@ to New Recipe %@", [newIngredient valueForKey:@"name"], [newRecipe valueForKey:@"name"]);
            }
    
    
        }
    
        return YES;
    }
    

    I’d post a picture of the setup in Xcode and the sample Xcode project, but I don’t seem to have any reputation points on stack overflow yet… so it won’t let me. I’ll post this to my blog as well. bingosabi.wordpress.com/.

    Also note that the Xcode Core Data model mapping stuff is a bit flaky and occasionally needs a “clean”, good Xcode rester, simulator bounce or all of the above get it working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.