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 8174599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:30:09+00:00 2026-06-06T22:30:09+00:00

I’m trying to make a database upgrade using a custom NSEntityMigrationPolicy . This is

  • 0

I’m trying to make a database upgrade using a custom NSEntityMigrationPolicy. This is how my data model looks like now and what is the intended result :

  A <-------->> B   ( 1 A -> many B ) 

I want a new C entity which will “break” the B type objects in some subsets :

  A <-------->> C <------->> B  ( 1 A can have many C, each C can have many B )

I made a custom NSEntityMigrationPolicy for entity A. I created some C entities for each A in createDestinationInstancesForSourceInstance:entityMapping:manager:error: , but I’m stuck what to do with the relation between A and B.

In docs they say to use createRelationshipsForDestinationInstance somehow to create the new relations. I haven’t found any suggestive examples however for this case. I suppose probably the A <->> C relation can be created here, but the B’s may not exist yet in the destination context to create a C<–>>B relation yet. How can a migration like this be done? Please, help 🙂

Thanks !

  • 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-06T22:30:11+00:00Added an answer on June 6, 2026 at 10:30 pm

    The basic approach would be

    1. Create a policy for migrating A-to-C
    2. Create a policy for migrating A-to-A
    3. Create a policy for migrating B-to-B

    It’s important to do it in the correct order.

    As C does not exist in the database you have to do a little trick: The NSMigrationManager can store a userInfo. This userInfo dictionary is available throughout the whole migration process. Therefore you can store every C object you’ve created in A-to-C in the userInfo dict. You need to have a unique identifier attribute on A to store this as the key and set the appropriate C object as the value. In the third migration (B-to-C) you can then access the appropriate C object and assign it to B.

    1. A-to-C: Creates the new C objects based on every existing A object.

    In beginEntityMapping:manager:error: create the your initial NSMutableDictionary and assign it to the NSMigrationManager‘s userInfo property.

    In createDestinationInstancesForSourceInstance:entityMapping:manager:error: do the following:

    - (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
    {
        NSMutableDictionary *cObjects = (NSMutableDictionary *)[manager userInfo];
        NSObject *aUniqueIdentifierForEveryAObject = [sInstance valueForKey:@"aUniqueIdentifier"];
    
        NSManagedObject *destinationC = nil;
        if ([[cObjects allKeys] containObject:aUniqueIdentifierForEveryAObject])
        {
            newC = [userInfo objectForKey:aUniqueIdentifierForEveryAObject];
        }
        else
        {
            newC = [NSEntityDescription insertNewObjectForEntityForName:@"C" inManagedObjectContext:[manager destinationContext]];
    
            // Assign instance A (sInstance) to the newC and update the userInfo
            [newC setValue:sInstance forKey:@"a"];
            [cObjects setObject:newC forKey:aUniqueIdentifierForEveryAObject];
        }
    
        [manager associateSourceInstance:sInstance withDestinationInstance:destinationC forEntityMapping:mapping];
        return YES;
    }
    
    - (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject*)dInstance entityMapping:(NSEntityMapping*)mapping manager:(NSMigrationManager*)manager error:(NSError**)error
    {
        return YES;
    }
    

    2. A-to-A: Migrates all A objects

    Here you can do some additional adjustments to A’s attributes. It’s important, that you create this migration whether you have attribute changes or not, as automatic migration is turned off. If you do not have any changes, you can simply create an Entity Mapping in the mapping model editor.

    3. B-to-B: Migrates all B objects and assigns new C objects

    Assign the new C objects. You cannot to this in createRelationships... as you need the A object and it’s unique identifier to get the appropriate C from the userInfo dict.

    - (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
    {
        id destinationB = [NSEntityDescription insertNewObjectForEntityForName:@"B" inManagedObjectContext:[manager destinationContext]];
        NSMutableDictionary *userInfo = (NSMutableDictionary *)[manager userInfo];
        NSMutableDictionary *cObjects = (NSMutableDictionary *)[userInfo valueForKey:kCObjects];
        A *anA = [sInstance valueForKey:@"a"];
        NSObject *aUniqueIdentifierForA = [anA valueForKey:@"aUniqueIdentifier"];
        C *newC = [userInfo objectForKey:aUniqueIdentifierForA];
        [destinationB setValue:newC forKey:@"c"];
    
        // Migrate all other attributes here
        [destinationB setValue:... forKey:...];
    
        [manager associateSourceInstance:sInstance destinationB forEntityMapping:mapping];
    
        return YES;
    }
    
    - (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject *)dInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
    {
        return YES;
    }
    

    I’ve not tested this but the basic approach should work.


    UPDATE

    There was a mistake in Step 1: A-to-C.

    // WRONG
    [cObjects setObject:newC forKey:sInstance];
    
    // UPDATED
    [newC setValue:sInstance forKey:@"a"];
    [cObjects setObject:newC forKey:aUniqueIdentifierForEveryAObject];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.