mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
18 lines
894 B
Swift
18 lines
894 B
Swift
import CoreData
|
|
|
|
extension NSManagedObjectContext {
|
|
/// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given
|
|
/// managed object context up to date.
|
|
///
|
|
/// Credit: https://www.avanderlee.com/swift/nsbatchdeleterequest-core-data/
|
|
///
|
|
/// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute.
|
|
/// - Throws: An error if anything went wrong executing the batch deletion.
|
|
public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws {
|
|
batchDeleteRequest.resultType = .resultTypeObjectIDs
|
|
let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult
|
|
let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []]
|
|
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self])
|
|
}
|
|
}
|