带有EXC_BAD_INSTRUCTION的CoreData错误(代码= EXC_I386_INVOP,子代码= 0x0) [英] CoreData error with EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

查看:163
本文介绍了带有EXC_BAD_INSTRUCTION的CoreData错误(代码= EXC_I386_INVOP,子代码= 0x0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开并发调试开关com.apple.CoreData.ConcurrencyDebug 1来跟踪CoreData的所有并发问题时,在调用insertingNewObjectForEntityForName时,我总是崩溃。

When I turn on the Concurrency debug switch 'com.apple.CoreData.ConcurrencyDebug 1' to track all concurrency issues with CoreData, I keep getting a crash when calling insertingNewObjectForEntityForName.

Xcode告诉我的消息是EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)。这是我的代码

The message Xcode shows me is EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). Here's my code

这是我对managedObjectContext的实现

Here's my implementation of managedObjectContext

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

这是[self privateContext]的实现

and here's the implementation of [self privateContext]

-(NSManagedObjectContext *)privateContext
{
    NSManagedObjectContext *pvtContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
pvtContext.parentContext = [CoreDataMgr sharedInstance].managedObjectContext;
return pvtContext;
}

场景1:在主线程上执行-不会崩溃

Scenario 1: executing on main thread - does not crash

NSManagedObjectContext *mainContext = [CoreDataMgr sharedInstance].managedObjectContext;
CDPayments* cdPayment = [NSEntityDescription insertNewObjectForEntityForName:PAYMENTS_TABLE inManagedObjectContext:mainContext];

场景2:在后台线程上执行-CRASHES !!

Scenario 2: Executing on background thread - CRASHES !!

NSManagedObjectContext *pvtContext = [self privateContext];
CDPayments* cdPayment = [NSEntityDescription insertNewObjectForEntityForName:PAYMENTS_TABLE pvtContext];

我真的不清楚为什么在带有私有上下文的后台线程上执行此操作会崩溃..

I'm really not clear why executing this on the background thread with a private context is crashing ...

我正在针对iOS9 SDK使用Xcode 8,并且在保存付款对象时调用了上面的代码。

I am using Xcode 8 against iOS9 SDK and the above code is called when saving a payment object.

推荐答案

这是因为您在执行Core Data并发错误。当使用 NSPrivateQueueConcurrencyType NSMainQueueConcurrencyType 时,必须将核心数据代码包装在对 perform() performAndWait()。如果不这样做,则您的代码违反了并发规则,完全可以预期会导致崩溃。

It's because you're doing Core Data concurrency wrong. When you use NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType, you must wrap your Core Data code in calls to perform() or performAndWait(). If you don't, your code is violating concurrency rules and this crash is completely expected.

唯一的例外是如果使用 NSMainQueueConcurrencyType ,您确定,该代码正在主队列上运行,则可以直接进行Core Data调用,而无需将它们包装在块中。

The only exception to this is if you use NSMainQueueConcurrencyType and you are certain that the code is running on the main queue, you can make the Core Data calls directly, without wrapping them in blocks.

这篇关于带有EXC_BAD_INSTRUCTION的CoreData错误(代码= EXC_I386_INVOP,子代码= 0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆