eamo
eamo

Reputation: 355

Trouble adding Core Data to a Storyboard App

I am having trouble adding a Core Data functionality to my app.

I cannot understand why managedObjectContext is always nil (even in my AppDelegate). I know that I should be passing it from the model, but and not sure how to do this.

I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Goal''

g4tappDelegate.h

#import <UIKit/UIKit.h>
#import "Goal.h"
@class g4tPopPageViewController;

@interface g4tAppDelegate : UIResponder <UIApplicationDelegate> {

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

    UIWindow *window;

}

- (NSManagedObjectContext *)managedObjectContext;

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) g4tPopPageViewController *PopPageViewController;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

g4tappDelegate.m

#import "g4tAppDelegate.h"
    #import "g4tPopPageViewController.h"
    #import "Goal.h"

    @implementation g4tAppDelegate
    NSManagedObjectContext *managedObjectContext;

    @synthesize PopPageViewController;
    @synthesize managedObjectContext = _managedObjectContext;
    @synthesize managedObjectModel = _managedObjectModel;
    @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        NSManagedObject *newGoal;
//ERROR HERE
        newGoal = [NSEntityDescription 
                   insertNewObjectForEntityForName:@"Goal"
                   inManagedObjectContext:_managedObjectContext];

        PopPageViewController.managedObjectContext = self.managedObjectContext;

        UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"AddGoal"];

        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];

        [self.window setRootViewController:navigationController];
        [self.window makeKeyAndVisible];
        // Override point for customization after application launch.

        return YES;
    }

Upvotes: 0

Views: 127

Answers (1)

Mark McCorkle
Mark McCorkle

Reputation: 9414

Initialize your context first. Pass the context to the viewController instance first after creating it. Either you are missing some implementation or you didn't post it here.

Also, correct your property

@property (strong, nonatomic) PopPageViewController *g4tPopPageViewController;

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

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

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourStore.sqlite"];

    NSError *error = nil;
    _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;
}

// Then pass it to your other controller from your viewDidLoad
vc.managedObjectContext = _managedObjectContext;

Upvotes: 1

Related Questions