I'm using Core Data to locally persist results from a Web Services call. The web service returns the full object model for, let's say, "Cars" – could be about 2000 of them (and I can't make the Web Service return anything less than 1 or ALL cars.
The next time I open my application, I want to refresh the Core Data persisted copy by calling the Web Service for all Cars again, however to prevent duplicates I would need to purge all data in the local cache first.
Is there a quicker way to purge ALL instances of a specific entity in the managed object context (e.g. all entities of type "CAR"), or do I need to query them call, then iterate through the results to delete each, then save?
Ideally I could just say delete all where entity is Blah.
Best Solution
iOS 9 and later:
iOS 9 added a new class called
NSBatchDeleteRequest
that allows you to easily delete objects matching a predicate without having to load them all in to memory. Here's how you'd use it:Swift 5
Objective-C
More information about batch deletions can be found in the "What's New in Core Data" session from WWDC 2015 (starting at ~14:10).
iOS 8 and earlier:
Fetch 'em all and delete 'em all: