Default Context is nil! Did you forget to initialize the Core Data Stack? [MagicalRecord]
Asked Answered
M

2

5

I'm using MagicalRecord for the first time.

I've set it up like this:

[MagicalRecord setupCoreDataStackWithStoreNamed:@"test"];

where test is the filename of my Core data file ( test.xcdatamodeld ).

In my view controller where I want to use my core data I wrote this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // If there is no POI, create a new POI
    if (!_poi) {
        _poi = [POI MR_createEntity];
    }
    // If there is no POI rank(=category) create one
    if (!_poi.rank) {
        _poi.rank = [Rank MR_createEntity];
    }
}

Where I did

@Class POI; 

in the header file. Where POI and Rank are my coredata classes that are generated by xCode.

When I run this: I always get:

2014-08-08 14:52:05.509 test[41248:60b] *** Assertion failure in +[NSManagedObjectContext MR_defaultContext], /Users/x/Documents/xCode/test/test/Pods/MagicalRecord/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m:60
2014-08-08 14:52:05.512 test[41248:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Default Context is nil! Did you forget to initialize the Core Data Stack?'

This happens just after the init of my ViewController.

Can someone help me?

EDIT:

I installed it via Cocoapods:

Pod 'MagicalRecord'

My Appdelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Setup Reliant
    [self _reliantInit];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[HomeViewController alloc]init]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    //Setup MagicalRecord
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"test"];

    return YES;
}
Macneil answered 8/8, 2014 at 12:58 Comment(5)
Did you put the setup in AppDelegate.m under application:didFinishLaunchingWithOptionsMerwyn
Yup I will edit my question with the AppDelegate.Macneil
You are using it wrong. DataModel != persistent storeHerefordshire
I don't get what there is wrong? I just followed this tutorial raywenderlich.com/56879/magicalrecord-tutorial-iosMacneil
Are there any other messages in the console? If you break on the stack setup code and step through, does it all look good? Does your model have the right name?Sigismondo
S
15

You're doing things in the wrong order. viewDidLoad of the root view controller will be called before your core data setup code, since you're adding it to the window straight away. Move the magical record setup to the top of the app delegate method.

Sigismondo answered 8/8, 2014 at 13:49 Comment(2)
Thank you so much. In the tutorial they say put it before return yes. Never thought about the flow.Macneil
The tutorial uses storyboards, the flow is different thereSigismondo
T
1

For anyone running into this, the marked answer is correct, but in my case it was due to one of the root tabBarController's view controller having a property initialized via core data fetch, and this initialization is called before the famous ApplicationDidfinishLaunchingWithOptions method. A simple fix was to make it lazy so the initialization does not gets called before the property is beign called.

private var lists = MyCoreDataModelClass().someMethodFetchingStuff()

becomes

private lazy var lists = MyCoreDataModelClass(). someMethodFetchingStuff()

(And btw now that's it's lazy I could store an MyCoreDataModelClass instance in another property so i don't initialise it every time i need it)

Thomasenathomasin answered 4/8, 2017 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.