(快速3)父子上下文崩溃核心数据(libc ++ abi.dylib:以NSException(Recorded Frame)类型的未捕获异常终止)

编程入门 行业动态 更新时间:2024-10-07 20:26:46
本文介绍了(快速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)类型的未捕获异常终止)

本文发布于:2023-11-26 19:13:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   父子   异常   核心   快速

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!