Swift CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。 [英] Swift CoreData: error: Serious application error. Exception was caught during Core Data change processing.

查看:496
本文介绍了Swift CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS上编写一个程序而且非常竞赛我遇到了这个错误:

I am writing one program on iOS and very race I am facing this error:


2015-11-06 10:57: 24.289 NETFNET [2503:976392] CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误。 - [__ NSCFSet addObject:]:尝试使用userInfo插入nil(null)
2015-11-06 10:57:24.293 NETFNET [2503:976392] ***因未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFSet addObject:]:尝试插入nil'

2015-11-06 10:57:24.289 NETFNET[2503:976392] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null) 2015-11-06 10:57:24.293 NETFNET[2503:976392] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'

我想,我试图同时访问数据库主线程和后台线程。我已经看到很多针对Objective C的解决方案,但没有针对Swift的解决方案(我不知道Objective C ......)。不幸的是,我不知道如何使用Grand Central Dispatch,事实上,我的程序并不需要几个步骤(我的意思是它需要它,但是如果某个线程丢失一个函数的信息一次,没有什么不好的意思发生)。我只想在Swift 1或2上有稳定的程序,所以我会感谢任何帮助。

I am trying to access Data Base simultaneously, I think, from main and background threads. I have seen a lot of solutions for Objective C, but none for Swift (I don't know Objective C...). Unfortunately, I don't know how to work with Grand Central Dispatch and, in fact, my program does not really need several treads (I mean it need it, but if some thread loses info from one function for one time, nothing bad will happen). I just want to have stable program on Swift 1 or 2, so I will be thankful for any help.

推荐答案

你需要创建一个具有私有队列并发类型的私有NSManagedObjectContext,并在后台线程上运行时使用它来访问CoreData。

You need to create a private NSManagedObjectContext with private queue concurrency type and use it to access CoreData whenever operating on a background thread.

因此假设我需要在后台运行数据库操作,我可以将该工作发送到后台线程

So suppose I need to run a database operation on the background, I can dispatch that work to the background thread as

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
        //call your background operation.
    })

然后在后台操作中我可以创建一个私人NSManagedObjectContext as

Then in the background operation I can create a private NSManagedObjectContext as

let moc = … //Our primary context on the main queue

let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = moc

privateMOC.performBlock {
    //operations
    do {
        try privateMOC.save()
    } catch {
        fatalError("Failure to save context: \(error)")
    }
}

通过Apple的CoreData并发指南阅读,以便在多线程上实现核心数据操作之前获得良好的理解。

Read through Apple's CoreData Concurrency Guide to get a good understanding before implementing core data operations on multiple threads.

这篇关于Swift CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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