MagicalRecord saveWithBlock 崩溃

Posted

技术标签:

【中文标题】MagicalRecord saveWithBlock 崩溃【英文标题】:MagicalRecord saveWithBlock crash 【发布时间】:2014-06-25 13:48:09 【问题描述】:

我尝试在装有 ios 7 的设备上运行此代码(实际上它在模拟器上运行良好) 我使用 MR 2.2 并使用 mogenerator 生成 Core Data 类

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)

            NewsEntity *newsEntity = [NewsEntity MR_createInContext:localContext];
        newsEntity.newsId = jsonObject[@"News_Id"];

    
    completion:^(BOOL success, NSError *error)
        if (!success || nil != error)
        
          NSLog(@"MagicalRecord saveWithBlock error: %@", error.description);
        
        else
        
            [self updateUI];
        
    ];

而我在这行代码中遇到了异常

NewsEntity *newsEntity = [NewsEntity MR_createInContext:localContext];

好像是这样的

* thread #5: tid = 0x31b342, 0x3aaafcc0 libobjc.A.dylib`objc_exception_throw, queue = 'NSManagedObjectContext Queue', stop reason = breakpoint 3.3
  * frame #0: 0x3aaafcc0 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x2fd50030 CoreData`+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 204
    frame #2: 0x00053f02 TestApp`+[_NewsEntity insertInManagedObjectContext:](self=0x00be4480, _cmd=0x369db6dc, moc_=0x16789640) + 322 at _NewsEntity.m:28
    frame #3: 0x0014de3e TestApp`+[NSManagedObject(self=0x00be4480, _cmd=0x00975bb0, context=0x16789640) MR_createInContext:] + 138 at NSManagedObject+MagicalRecord.m:157
    frame #4: 0x00057df4 TestApp`__52-[NewsViewController saveDataToPersistentStorage:]_block_invoke(.block_descriptor=<unavailable>, localContext=0x16789640) + 472 at NewsViewController.m:115
    frame #5: 0x00144282 TestApp`__51+[MagicalRecord(.block_descriptor=<unavailable>) saveWithBlock:completion:]_block_invoke + 46 at MagicalRecord+Actions.m:28
    frame #6: 0x2fdaca70 

我也有这样的终止异常

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'NewsEntity' in this model.'

我只添加了 [MagicalRecord setupAutoMigratingCoreDataStack];到 AppDelegate

你能解决这个问题吗?

【问题讨论】:

【参考方案1】:

抱歉,我忘记将 Core Data 模型添加到 Target Membership

【讨论】:

以上是关于MagicalRecord saveWithBlock 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在并发 NSOperation (MagicalRecord-2.3) 中使用 MagicalRecord 的正确方法

MagicalRecord:后台保存

未找到 MagicalRecord/MagicalRecordDeprecationMacros.h 文件

WatchKit 和 MagicalRecord - 这可能吗?

MagicalRecord 3.0 设置

MagicalRecord 日期解析