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

  • Home
  • SEARCH
  • 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 5941509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:14+00:00 2026-05-22T16:07:14+00:00

My iPhone app needs to migrate its core data store, and some of the

  • 0

My iPhone app needs to migrate its core data store, and some of the databases are quite large. Apple’s documentation suggests using “multiple passes” to migrate data to reduce memory use. However, the documentation is very limited and doesn’t explain very well how to actually do this. Can someone either point me towards a good example, or explain in detail the process of how to actually pull this off?

  • 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-05-22T16:07:14+00:00Added an answer on May 22, 2026 at 4:07 pm

    I’ve figured out what Apple hints in their documentation. It’s actually very easy but a long way to go before it’s obvious. I’ll illustrate the explanation with an example. The initial situation is this:

    Data Model Version 1

    enter image description here
    enter image description here

    It’s the model you get when you create a project with the “navigation based app with core data storage” template. I compiled it and did some hard hitting with some help of a for loop to create around 2k entries all with some different values. There we go 2.000 events with an NSDate value.

    Now we add a second version of the data model, which looks like this:

    enter image description here

    Data Model Version 2

    The difference is: The Event entity is gone, and we’ve got two new ones. One which stores a timestamp as a double and the second one which should store a date as NSString.

    The goal is to transfer all Version 1 Events to the two new entities and convert the values along the migration. This results in twice the values each as a different type in a separate entity.

    To migrate, we choose migration by hand and this we do with mapping models. This is also the first part of the answer to your question. We will do the migration in two steps, because it’s taking long to migrate 2k entries and we like to keep the memory footprint low.

    You could even go ahead and split these mapping models further to migrate only ranges of the entities. Say we got one million records, this may crash the whole process. It’s possible to narrow the fetched entities down with a Filter predicate.

    Back to our two mapping models.

    We create the first mapping model like this:

    1. New File -> Resource -> Mapping Model
    enter image description here

    2. Choose a name, I chose StepOne

    3. Set source and destination data model

    enter image description here

    Mapping Model Step One

    enter image description here

    enter image description here

    enter image description here

    The multi pass migration doesn’t need custom entity migration policies, however we will do it to get a bit more detail for this example. So we add a custom policy to the entity. This is always a subclass of NSEntityMigrationPolicy.

    enter image description here

    This policy class implements some methods to make our migration happen. However it’s simple in this case so we will have to implement only one method: createDestinationInstancesForSourceInstance:entityMapping:manager:error:.

    The implementation will look like this:

    StepOneEntityMigrationPolicy.m

    #import "StepOneEntityMigrationPolicy.h"
    
    
    @implementation StepOneEntityMigrationPolicy
    
    - (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance 
                                          entityMapping:(NSEntityMapping *)mapping 
                                                manager:(NSMigrationManager *)manager 
                                                  error:(NSError **)error
    {
        // Create a new object for the model context
        NSManagedObject *newObject = 
            [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] 
                                          inManagedObjectContext:[manager destinationContext]];
    
        // do our transfer of nsdate to nsstring
        NSDate *date = [sInstance valueForKey:@"timeStamp"];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];    
    
        // set the value for our new object
        [newObject setValue:[dateFormatter stringFromDate:date] forKey:@"printedDate"];
        [dateFormatter release];
    
        // do the coupling of old and new
        [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];
    
        return YES;
    }
    

    Final step: the migration itself

    I’ll skip the part for setting up the second mapping model which is almost identical, just a timeIntervalSince1970 used to convert the NSDate to a double.

    Finally we need to trigger the migration. I’ll skip the boilerplate code for now. If you need it, I’ll post here. It can be found at Customizing the Migration Process it’s just a merge of the first two code examples. The third and last part will be modified as follows: Instead of using the class method of the NSMappingModel class mappingModelFromBundles:forSourceModel:destinationModel: we will use the initWithContentsOfURL: because the class method will return only one, maybe the first, found mapping model in the bundle.

    Now we’ve got the two mapping models which can be used in every pass of the loop and send the migrate method to the migration manager. That’s it.

    NSArray *mappingModelNames = [NSArray arrayWithObjects:@"StepOne", @"StepTwo", nil];
    NSDictionary *sourceStoreOptions = nil;
    
    NSURL *destinationStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataMigrationNew.sqlite"];
    
    NSString *destinationStoreType = NSSQLiteStoreType;
    
    NSDictionary *destinationStoreOptions = nil;
    
    for (NSString *mappingModelName in mappingModelNames) {
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:mappingModelName withExtension:@"cdm"];
    
        NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
    
        BOOL ok = [migrationManager migrateStoreFromURL:sourceStoreURL
                                                   type:sourceStoreType
                                                options:sourceStoreOptions
                                       withMappingModel:mappingModel
                                       toDestinationURL:destinationStoreURL
                                        destinationType:destinationStoreType
                                     destinationOptions:destinationStoreOptions
                                                  error:&error2];
        [mappingModel release];
    } 
    

    Notes

    • A mapping model ends in cdm in the bundle.

    • The destination store has to be provided and should not be the source store. You can after successful migration delete the old and rename the new one.

    • I did some changes to the data model after the creation of the mapping models, this resulted in some compatibility errors, which I could only solve with recreating the mapping models.

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

Sidebar

Related Questions

I am developing an iPhone app in which app needs to send some data
In an iPhone app I'm working on, the user needs to enter some configuration
I'm working on my 2nd iPhone app and am curious about Core Data. Time
We are developing an iphone app that needs to process audio data in real
I have built an iPhone app that needs to pull data from a server.
This is for an iPhone app which needs to send encrypted data to a
I have a form data from an iphone app that needs to send HTTP
I am developing an iphone app that needs to parse data received from server.
I've started a new iPhone app that needs to remember a single, 40-char string.
I'm working in a iPhone app that needs to send a array as a

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.