Attempting to use NSBatchInsertRequest
initialized with an entity name and a managedObjectHandler
like so:
let insertRequest = NSBatchInsertRequest(entityName: "Application") { (object: NSManagedObject) in
object.setValue("My Application", forKey: "name")
return true
}
insertRequest.resultType = .objectIDs
does not do anything on macOS (12.5) for me. The execution of the request is done like this:
do {
let viewContext = library.persistentContainer.viewContext
let insertResult = try viewContext.execute(insertRequest) as? NSBatchInsertResult
if let objectIDs = insertResult?.result as? [NSManagedObjectID] {
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: [NSInsertedObjectsKey: objectIDs], into: [viewContext])
}
} catch let error {
// handle err
}
The problem is that objectIDs
is always empty and thus nothing gets inserted. However, when I switch to init'ing an NSBatchInsertRequest
using an objects array, everything works fine.
Despite having Core Data SQL logging enabled and searching inside the Console, no errors are thrown or any indicator as to why the insertion has failed.
Is it possible that this method is not really implemented on macOS and only works on other platforms such as iOS?
Note: I'm aware that performance wise a batch insertion of objects done on the main context is not ideal.