Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application crashes when executing NSBatchDeleteRequest with "Unknown Command Type <NSBatchDeleteRequest,..>"

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.

like image 868
Albertsen Avatar asked Dec 12 '15 10:12

Albertsen


2 Answers

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.

like image 150
Rory Prior Avatar answered Nov 16 '22 03:11

Rory Prior


NSBatchDeleteRequest is executed on the persistent store coordinator, not the managed object context.

try persistentStoreCoordinator.executeFetchRequest(
    batchDeleteRequest, withContext:context
)
like image 30
Mundi Avatar answered Nov 16 '22 04:11

Mundi