Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

18 řádky
894 B

  1. import CoreData
  2. extension NSManagedObjectContext {
  3. /// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given
  4. /// managed object context up to date.
  5. ///
  6. /// Credit: https://www.avanderlee.com/swift/nsbatchdeleterequest-core-data/
  7. ///
  8. /// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute.
  9. /// - Throws: An error if anything went wrong executing the batch deletion.
  10. public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws {
  11. batchDeleteRequest.resultType = .resultTypeObjectIDs
  12. let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult
  13. let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []]
  14. NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self])
  15. }
  16. }