views:

33

answers:

2

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]];
}
A: 

Show your code where you set up Core Data, especially the managed object context. If you are creating your backing store inside the application bundle, this will succeed on the Simulator but this will fail on the device. (As one example of what could be wrong.)

Shaggy Frog
Updated question with Core Data stack.
Maverick
A: 

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;
    }    
}
Maverick