2

I'm working with Core Data and having a lot of trouble's getting data into the database right off the start of my app.
Below is some of the code I've grabbed from a tutorial I followed. The point where I get the SIGABRT is outlined below. Any suggestions or help is appreciated Thanks

//THIS FUNCTION IS CALLED AFTER MY APP DID FINISHING LOADING IN THE 
//    IN THE APP DELEGATE
-(void)loadData
{
    NSManagedObjectContext *context = [self managedObjectContext];

    NewModel *newModel = (NewModel *)[NSEntityDescription insertNewObjectForEntityForName:@"NewModel" inManagedObjectContext:context];

    //ADD MORE DATA TO ENTITY
}

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    else
    {
        NSLog(@"Error");
    }        
    return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
/**********************************************************/    
    //  SIGABRT HAPPENS IN THE NEXT LINE OF CODE
/**********************************************************/    
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];    
    return managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:@"NewModel.db"];
    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

    // Put down default db if it doesn't already exist
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"LeagueModel" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;    
}

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Vince613
  • 329
  • 7
  • 18

1 Answers1

0

if you check the apple's documentation:

mergedModelFromBundles:

Returns a model created by merging all the models found in given bundles.

+ (NSManagedObjectModel *)mergedModelFromBundles:(NSArray *)bundles

Parameters

***bundles***

An array of instances of NSBundle to search. If you specify nil, then the main bundle is searched.

***Return Value***

A model created by merging all the models found in bundles.

you are passing nil where you need to pass the array for bundles this is the reason for crash.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41