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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:09:58+00:00 2026-05-17T03:09:58+00:00

I’m using Core Data and my app works perfectly in the simulator but on

  • 0

I’m using Core Data and my app works perfectly in the simulator but on the actual device the database is empty.

I enabled CoreData logging, so I see the actual tables being created, and queries run but all the queries are empty. I also copied the app from the device and verified that the schema is created but with no data.

What am I missing for the data to actually get re-populated?

UPDATED WITH CODE

- (NSDictionary*)migrationOptions {
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];   
    return options;
}

- (NSManagedObjectContext*)managedObjectContext {
    if( _managedObjectContext != nil ) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
        [_managedObjectContext setUndoManager:nil];
        [_managedObjectContext setRetainsRegisteredObjects:YES];
    }
    return _managedObjectContext;
}


- (NSManagedObjectModel*)managedObjectModel {
    if( _managedObjectModel != nil ) {
        return _managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];

    //_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return _managedObjectModel;
}


- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
    if( _persistentStoreCoordinator != nil ) {
        return _persistentStoreCoordinator;
    }

    NSString* storePath = [self storePath];
    NSURL *storeUrl = [self storeUrl];


    NSError* error;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSDictionary* options = [self migrationOptions];

    // Check whether the store already exists or not.
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL exists = [fileManager fileExistsAtPath:storePath];

    TTDINFO(storePath);
    if( !exists ) {
        _modelCreated = YES;
    } else {
        if( _resetModel ||
           [[NSUserDefaults standardUserDefaults] boolForKey:@"erase_all_preference"] ) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"erase_all_preference"];
            [fileManager removeItemAtPath:storePath error:nil];
            _modelCreated = YES;
        }
    }

    if (![_persistentStoreCoordinator
          addPersistentStoreWithType: kStoreType
          configuration: nil
          URL: storeUrl
          options: options
          error: &error
          ]) {
        // We couldn't add the persistent store, so let's wipe it out and try again.
        [fileManager removeItemAtPath:storePath error:nil];
        _modelCreated = YES;

        if (![_persistentStoreCoordinator
              addPersistentStoreWithType: kStoreType
              configuration: nil
              URL: storeUrl
              options: nil
              error: &error
              ]) {
            // Something is terribly wrong here.
        }
    }

    return _persistentStoreCoordinator;
}


- (NSString*)storePath {
    return [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kStoreFilename];
}


- (NSURL*)storeUrl {
    return [NSURL fileURLWithPath:[self storePath]];
}
  • 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-17T03:09:59+00:00Added an answer on May 17, 2026 at 3:09 am

    Ok, so after so more searching finally got the solution.

    Basically it’s to copy the original sql file from simulator to the device.

    from http://iphonedevelopment.blogspot.com/2010/08/core-data-starting-data.html

    here’s the Persistant Store Coordinator creation:

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
    {
        @synchronized (self)
        {
            if (persistentStoreCoordinator != nil)
                return persistentStoreCoordinator;
    
            NSString *defaultStorePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"My_App_Name" ofType:@"sqlite"];
            NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"My_App_Name.sqlite"];
    
            NSError *error;
            if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) 
            {
                if ([[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:storePath error:&error])
                    NSLog(@"Copied starting data to %@", storePath);
                else 
                    NSLog(@"Error copying default DB to %@ (%@)", storePath, error);
            }
    
            NSURL *storeURL = [NSURL fileURLWithPath:storePath];
    
            persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                     [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    
            if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
            {
    
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }    
    
            return persistentStoreCoordinator;
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.