为什么删除后会引发Core Data NSManagedObject故障? [英] Why are Core Data NSManagedObject faults fired upon deletion?

查看:68
本文介绍了为什么删除后会引发Core Data NSManagedObject故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试有效地批量删除很多 NSManagedObject s(不使用 NSBatchDeleteRequest )。我一直在此答案(适用于Swift)中按照一般过程进行操作,方法是批量处理请求对象,删除,保存,然后重置上下文。我的提取请求设置了 includesPropertyValues false

I'm trying to efficiently batch delete a lot of NSManagedObjects (without using an NSBatchDeleteRequest). I have been following the general procedure in this answer (adapted to Swift), by batching an operation which requests objects, deletes, saves and then resets the context. My fetch request sets includesPropertyValues to false.

但是,在运行时,从上下文中删除每个对象时,故障被触发。添加日志记录如下:

However, when this runs, at the point where each object is deleted from the context, the fault is fired. Adding logging as follows:

// Fetch one object without property values
let f = NSFetchRequest<NSManagedObject>(entityName: "Entity")
f.includesPropertyValues = false
f.fetchLimit = 1

// Get the result from the fetch. This will be a fault
let firstEntity = try! context.fetch(f).first!

// Delete the object, watch whether the object is a fault before and after
print("pre-delete object is fault: \(firstEntity.isFault)")
context.delete(firstEntity)
print("post-delete object is fault: \(firstEntity.isFault)")

产生输出:


预删除对象是错误的:true

后删除对象是错误的:false

即使没有重写任何CoreData方法( willSave() prepareForDeletion() validateForUpdate()等)。我不知道还会导致这些错误的其他原因。

This occurs even when there are no overrides of any CoreData methods (willSave(), prepareForDeletion(), validateForUpdate(), etc). I can't figure out what else could be causing these faults to fire.

更新:我创建了一个简单的示例。它具有具有单个属性的单个实体,并且没有关系。游乐场从 NSPersistentContainer 的viewContext中删除主线程上的托管对象,这表明对象属性 isFault true 更改为 false

Update: I've created a simple example in a Swift playground. This has a single entity with a single attribute, and no relationships. The playground deletes the managed object on the main thread, from the viewContext of an NSPersistentContainer, a demonstrates that the object property isFault changes from true to false.

推荐答案

我认为权威的答案需要看一下Core Data源代码。由于这不太可能实现,因此有一些我可以想到的理由。

I think an authoritative answer would require a look at the Core Data source code. Since that's not likely to be forthcoming, here are some reasons I can think of that this might be necessary.


  • 对于具有关系的实体,可能有必要检查关系以处理删除规则并维护数据完整性。例如,如果删除规则为级联,则有必要触发错误以找出应该删除哪些相关实例。如果它是 nullify,则触发错误以找出哪些相关实例需要将其关系值设置为nil。

  • 除上述内容外,具有关系的实体还需要进行验证检查在相关实例上执行。例如,如果您删除具有使用 nullify删除规则的关系的对象,并且反向关系不是可选的,则将无法对反向关系进行验证检查。

  • 二进制属性可以将数据自动存储在外部文件中(允许外部存储选项)。为了清理外部文件,可能有必要触发该错误,以便知道要删除哪个文件。

I认为所有这些都可能会被优化掉。例如,如果实体没有关系并且没有使用外部存储的属性,请不要触发错误。但是,这是从外部看的,无法访问源代码。可能还有其他原因需要触发故障。这似乎有可能。或者可能是由于任何原因没有人尝试进行此优化。

I think all of these could probably be optimized away. For example, don't fire faults if the entity has no relationships and has no attributes that use external storage. However, this is looking from the outside without access to source code. There might be other reasons that require firing the fault. That seems likely. Or it could be that nobody has attempted this optimization, for whatever reason. That seems less likely but is possible.

顺便说一句,我分叉了操场代码以获取一个不依赖外部数据模型文件的版本,而是通过代码构建模型。

BTW I forked your playground code to get a version that doesn't rely on an external data model file, but instead builds the model in code.

这篇关于为什么删除后会引发Core Data NSManagedObject故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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