本文介绍了(快速3)父子上下文崩溃核心数据(libc ++ abi.dylib:以NSException(Recorded Frame)类型的未捕获异常终止)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用父级和子级两个上下文将消息保存在后台队列中,并将其推送到主队列中。但我的应用不断崩溃。我用过苹果文档,不确定为什么它不起作用...



解决方案

好,所以您有两个问题。



第一个是您尚未设置父上下文。如果您不这样做,则不会传播任何内容到您的主托管上下文中。



第二个原因是您不会在自己的块中更改私有托管上下文。



所以完成的代码应该看起来像这样,就像正常的托管上下文需要运行(已经完成)一样,私有上下文也需要这样做。此:-

  let context =(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext 

let privateMOC = NSManagedObjectContext(concurrencyType:.privateQueueConcurrencyType)

private.parentContext =上下文

let doubletimestamp = Double(timestamp)
let date = Date(timeIntervalSinceReferenceDate :((doubletimestamp))
let status = ...

privateMOC.performBlock {

let message = NSEntityDescription.insertNewObject(forEntityName: Mesages,进入:self.privateMOC)作为!消息
message.text =文本
message.timestamp =日期为NSDate

do {
try self.privateMOC.save()
self.inputToolbar .toggleSendButtonEnabled()

self.context.performAndWait {

do {
try self.context.save()
} catch {
fatalError(无法保存上下文:\(错误))
}

}

} catch let err {
print(err )
}

}
}


I'm trying to save messages on the background queue and push them to the main queue by using two contexts a parent and a child. but my app keeps crashing. I used apples docs, not sure why its not working...

Core Data, Multithreading, and the Main Thread

here is my code:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)


let doubletimestamp = Double(timestamp)
    let date = Date(timeIntervalSinceReferenceDate: (doubletimestamp))
    let status = "..."

    let message = NSEntityDescription.insertNewObject(forEntityName: "Mesages", into: self.privateMOC) as! Mesages
    message.text = text
    message.timestamp = date as NSDate

    do {
        try self.privateMOC.save()
        self.inputToolbar.toggleSendButtonEnabled()

        self.context.performAndWait {

            do {
                try self.context.save()
            } catch {
                fatalError("Failure to save context: \(error)")
            }

        }

    }catch let err {
        print(err)
    }

    }

also here is my stacktrace:

解决方案

Ok so there are two problems you have.

The first is that you haven't set the parent context. If you don't do this nothing will be propagated to your main managed context

The second is that you are not changing the private managed context in it's own block. In the same way that your normal managed context needs to be run (which you have done) the private context needs to do the same.

So the finished code should look like this:-

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

private.parentContext = context

let doubletimestamp = Double(timestamp)
let date = Date(timeIntervalSinceReferenceDate: (doubletimestamp))
let status = "..."

privateMOC.performBlock {

    let message = NSEntityDescription.insertNewObject(forEntityName: "Mesages", into: self.privateMOC) as! Mesages
    message.text = text
    message.timestamp = date as NSDate

    do {
        try self.privateMOC.save()
        self.inputToolbar.toggleSendButtonEnabled()

        self.context.performAndWait {

            do {
                try self.context.save()
            } catch {
                fatalError("Failure to save context: \(error)")
            }

        }

    }catch let err {
        print(err)
    }

    }
}

这篇关于(快速3)父子上下文崩溃核心数据(libc ++ abi.dylib:以NSException(Recorded Frame)类型的未捕获异常终止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:32