【发布时间】:2014-10-01 23:16:39
【问题描述】:
我是第一次使用 MagicalRecord。
我是这样设置的:
[MagicalRecord setupCoreDataStackWithStoreNamed:@"test"];
其中 test 是我的核心数据文件 ( 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 生成的核心数据类。
当我运行这个时:我总是得到:
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;
}
【问题讨论】:
-
你有没有把设置放在
AppDelegate.m下application:didFinishLaunchingWithOptions下 -
是的,我将使用 AppDelegate 编辑我的问题。
-
你用错了。 DataModel != 持久存储
-
我不明白有什么问题?我只是按照这个教程raywenderlich.com/56879/magicalrecord-tutorial-ios
-
控制台中还有其他消息吗?如果您打破堆栈设置代码并单步执行,一切看起来都不错吗?你的模型有正确的名字吗?
标签: ios objective-c core-data magicalrecord