gpt4 book ai didi

ios - EXC_???在执行 executeFetchRequest 时

转载 作者:行者123 更新时间:2023-11-29 13:19:11 24 4
gpt4 key购买 nike

我正在尝试将外部数据添加到 SQLite 中/使用 Core Data 更新现有数据。

基本上,我从外部 Web 服务获得了一个 JSON,我正在使用以下代码来确定我是应该在数据库中添加新对象还是更新现有对象。

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"name" inManagedObjectContext:context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"id = %@", [data valueForKey:@"key"]]];
NSArray *results = [context executeFetchRequest:fetch error:nil];

if (results.count == 1)
{
// update existing
}
else
{
// add new
}

问题是:有时这段代码会导致异常:

Thread 1: EXC_??? (11) (code=0, subcode=0x0)

NSManagedObjectContext executeFetchRequest:error: 中引发异常

如果我继续执行我的应用程序,一切似乎都正常。

我应该担心这个异常吗?

我的意思是拥有它有点烦人,但更重要的是了解此异常的原因和后果是什么?

一些额外的细节(以防万一):

  • 上面的代码在循环中执行了多次(大约 25 万次)。
  • 代码在主线程上运行(是的,我知道,但它是一种原型(prototype))。
  • 上下文是在主线程上创建的。
  • 外部数据在后台线程中创建

[编辑]更多细节:

  • executeFetchRequest:error 即使在引发异常时也返回初始化数组。
  • 当我向 executeFetchRequest:error 提供 error 参数时没有设置错误

最佳答案

那不安全。

您应该检查方法的返回以确保您返回了一个数组

NSArray *results = [context executeFetchRequest:fetch error:nil];
if (!results) {
// An error occurred you should probably use the out error
}

而且 CoreData 似乎在内部抛出异常但会处理它们,因此如果您设置了异常断点,它很可能会在 CoreData 堆栈的随机点被捕获 - 我是根据过去的经验说的,不确定它是否在任何地方记录但在这个视频中提到了Debugging Tips - Mike Hay

关于ios - EXC_???在执行 executeFetchRequest 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14812611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com