默认上下文为零!您是否忘记初始化核心数据堆栈?[MagicalRecord]

use*_*522 5 core-data objective-c ios magicalrecord

我第一次使用MagicalRecord.

我把它设置成这样:

[MagicalRecord setupCoreDataStackWithStoreNamed:@"test"];
Run Code Online (Sandbox Code Playgroud)

其中test是我的Core数据文件(test.xcdatamodeld)的文件名.

在我的视图控制器中,我想要使用我的核心数据,我写了这个:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

我在哪里

@Class POI; 
Run Code Online (Sandbox Code Playgroud)

在头文件中.POI和Rank是我的xCode生成的coredata类.

当我跑这个:我总是得到:

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?'
Run Code Online (Sandbox Code Playgroud)

这发生在我的ViewController初始化之后.

有人能帮我吗?

编辑:

我是通过Cocoapods安装的:

Pod 'MagicalRecord'
Run Code Online (Sandbox Code Playgroud)

我的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;
}
Run Code Online (Sandbox Code Playgroud)

jrt*_*ton 14

你做的事情是错误的.viewDidLoad在您的核心数据设置代码之前,将调用根视图控制器,因为您将立即将其添加到窗口中.将魔法记录设置移动到app delegate方法的顶部.