I'm trying to efficiently batch delete a lot of NSManagedObject
s (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)")
yields the output:
pre-delete object is fault: true
post-delete object is fault: false
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.
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
.
List
) has a many-to-many relationship toBook
entities, with a delete rule ofnullify
. (Book
objects also have relationships to another entity, but I didn't think that would matter). I put in therelationshipKeyPathsForPrefetching
so that thebooks
relationship would be prefetched. I thought - based on this answer - that that would be enough, and no faults would need to be fired upon delete... – BronwenNSBatchDeleteRequest
(the obvious alternative) requires me to trigger an update to my table views:NSFetchedResultsControllerDelegate
is not notified of changes. But perhaps it would be easier just to implement a batch delete notification which triggers a tableView reload... – Bronwen