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

 eggplant 发布于 2022-12-29 10:53

我第一次使用MagicalRecord.

我把它设置成这样:

[MagicalRecord setupCoreDataStackWithStoreNamed:@"test"];

其中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];
    }
}

我在哪里

@Class POI; 

在头文件中.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?'

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

有人能帮我吗?

编辑:

我是通过Cocoapods安装的:

Pod 'MagicalRecord'

我的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;
}

jrturton.. 14

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

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

    2022-12-29 10:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有