问题描述
我正在尝试使用NSBatchDeleteRequest删除一堆实体,其中许多实体具有删除级联和/或使规则无效的方法。
I'm attempting to use NSBatchDeleteRequest to delete a pile of entities, many of these entities have delete cascade and/or nullify rules.
我第一次尝试删除一切失败,我返回的NSError包含字符串批量删除不支持删除规则。我本以为可以删除此类内容,但是我有责任确保在保存之前满足所有约束条件。
My first attempt to delete anything fails and the NSError I get back includes the string "Delete rule is not supported for batch deletes". I had thought it was fine to delete such things but i was responsible for making sure all the constraints are satisfied before I do a save.
我应该能够批量删除这些托管对象? (我想保留删除规则,其他删除路径没有一种简单的方法来知道要删除的对象集)在这种情况下,某些类型的批处理删除有效吗? (例如谓词失败,但是对象ID列表起作用吗?)
Should I be able to batch delete these managed objects? (I want to keep the delete rules, other delete paths don't have an easy way to know what set of objects to delete) Do some kinds of batch deletes work in this case, but others not? (say predicates fail, but a list of object IDs work?)
推荐答案
批量删除对于关系而言是有问题的。
Batch delete is problematic with relationships.
它直接进入数据库并删除暂停所有对象图规则(包括删除规则)的记录。您已经正确确定了需要再次进行所有约束检查的要求。 (这本身可能是一个破坏交易的行为。)
It goes directly to the database and deletes the records suspending all object graph rules, including the delete rules. You have correctly identified the requirement that you need to do all the constraint checking yourself again. (That by itself could be a deal-breaker.)
即使您设法正确删除实体和所有必要的相关实体,您仍然会留下很多东西(不透明的)联接表中的条目的数量在后台创建。没有明显的安全方法可以删除联接表中的条目,并且据报告它们会干扰将来操作中的关系管理。
Even if you manage to delete the entities and all the necessary related entities correctly, you will still be left with lots of entries in the (opaque) join table Core Data creates in the background. There is no obvious safe way to delete the entries in the join tables and they have been reported to interfere with managing relationships in future operations.
IMO,这种情况下的解决方案仍然是使用对象图,而不是批量删除并优化性能 >。关于SOF,如何执行此操作有很多好的答案,但其中的大多数都可以归纳为以下几点:
IMO , the solution in this case is to still use the object graph rather than batch delete and optimize for performance. There are many good answers on SOF on how to do this, but most of it can be summarized with these points:
- 找到正确的批次保存的大小(通常创建500个实体,删除大约2000个实体,但这可能会根据对象的大小和关系的复杂性而有所不同-您必须进行试验)。
- 如果您有内存限制,使用
autoreleasepool
s。 - 使用后台上下文释放UI进行交互。我更喜欢在更新UI后在后台将其保存到数据库中。
- find the right batch size for saving (typically 500 entities for creation, about 2000 for deletion, but this could vary according to object size and relationship complexity - you have to experiment).
- if you have memory constraints, use
autoreleasepool
s. - use a background context to free the UI for interaction. I prefer to do the saving to the database in the background after updating the UI.
这篇关于我可以在具有删除规则的关系的实体上使用NSBatchDeleteRequest吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!