NSBatchDeleteRequest

Here’s an example:

let batchDeleteFetchRequest = NSFetchRequest(entityName: "Thing")
batchDeleteFetchRequest.sortDescriptors = [NSSortDescriptor(key: "uri", ascending: true)]
batchDeleteFetchRequest.predicate = NSPredicate(format: "NOT (uri IN %@)", URIsInDataDump)
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: batchDeleteFetchRequest)
        
do {
    try managedObjectContext.executeRequest(batchDeleteRequest)
} catch {
    self.logger.error("-15")
}

There isn’t much official documentation out there because NSBatchDeleteRequest doesn’t exist in documentation, only a video clip from Core Data’s spotlight at WWDC.

Anyway, when running the above code and watching the context, you’ll notice no changes were acknowledged, though loading a new fetch does show the items were deleted. What?

Well, NSBatchDeleteRequest works by deleting directly from the store and even though it’s the managedObjectContext executing the request, it happens outside of its knowledge and therefore, isn’t aware of the persistent store change.

This can be fixed, though, by telling your Managed Object Contexts to automatically consume changes from parent.

I am Dan Griffin and you can find me on Mastodon

The Blog

Base & Elevated System (and Grouped!) Background Colors

In iOS 13, Apple introduced a slew of new colors that are also dynamic – meaning they will adjust between light and dark modes (and other scenarios, such as high contrast). Of the new colors, the various background colors are pretty pecular: iOS defines two sets of background colors—system and grouped—each of which contains primary,…

iOS iOS 13

Always Taking Inquiries

At the moment I am not taking on many new projects, but am still available for inquiry or questions.

Reach Out To Dan