Skip to content

Commit

Permalink
Remove batch delete
Browse files Browse the repository at this point in the history
  • Loading branch information
acwright committed Nov 13, 2019
1 parent ddf0ed9 commit 64de93d
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions Sources/Cabinet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,25 @@ public extension Cabinet {
return object.id
}

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: I.entityName)
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: I.entityName)
fetchRequest.predicate = NSPredicate(format: "id IN %@", ids)

let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
batchDeleteRequest.resultType = .resultTypeObjectIDs

do {
let result = try managedObjectContext.execute(batchDeleteRequest) as! NSBatchDeleteResult
let changes: [AnyHashable: Any] = [
NSDeletedObjectsKey: result.result as! [NSManagedObjectID]
]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [managedObjectContext])

try I.cascades.forEach { cascade in
let childFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: cascade)
childFetchRequest.predicate = NSPredicate(format: "%K IN %@", I.foreignKey, ids)

let childBatchDeleteRequest = NSBatchDeleteRequest(fetchRequest: childFetchRequest)
childBatchDeleteRequest.resultType = .resultTypeObjectIDs

let childResult = try managedObjectContext.execute(childBatchDeleteRequest) as! NSBatchDeleteResult
let childChanges: [AnyHashable: Any] = [
NSDeletedObjectsKey: childResult.result as! [NSManagedObjectID]
]
let asynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { (asynchronousFetchResult) in
guard let result = asynchronousFetchResult.finalResult else {
DispatchQueue.main.async { completion(.success(false)) }
return
}

NSManagedObjectContext.mergeChanges(fromRemoteContextSave: childChanges, into: [managedObjectContext])
DispatchQueue.main.async {
result.forEach { object in
managedObjectContext.delete(object)
}
completion(.success(true))
}

completion(.success(true))
}

do {
try managedObjectContext.execute(asynchronousFetchRequest)
} catch {
completion(.failure(error))
}
Expand Down

0 comments on commit 64de93d

Please sign in to comment.