I'm writing an OS X application, and it needs to be able to delete all "SongEntity" instances held in its Core Data store. However, when I try to execute an NSBatchDeleteRequest, my application crashes, with the following console-output (excerpt):
Unknown command type (entity: SongEntity; predicate: ((null)); sortDescriptors: ((null)); type: NSManagedObjectIDResultType; ) >
Here's my implementation:
func clearStore()
{
let fetchRequest = NSFetchRequest(entityName: "SongEntity")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do
{
try managedObjectContext.executeRequest(deleteRequest)
}
catch
{
fatalError("Not able to perform operation: \(error)")
}
managedObjectContext.reset()
}
Any help would be much appreciated
EDIT:
It turns out that this issue is related to the chosen store-type:
From class NSBatchDeleteRequest
:
// May not be supported by all store types.
I tried changing the store-type from NSXMLStoreType
(the macOS template default) to NSSQLiteStoreType
and now it works.
I ran into this problem too using NSInMemoryStoreType on my persistent store. It turns out that not all store types support batch deletes, so I had to switch to using a fetch request and simply iterating over the managed objects and removing them one by one.
NSBatchDeleteRequest
is executed on the persistent store coordinator, not the managed object context.
try persistentStoreCoordinator.executeFetchRequest(
batchDeleteRequest, withContext:context
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With