Skip to content

Commit

Permalink
Merge branch 'master' of github.com:infinitetoken/Cabinet
Browse files Browse the repository at this point in the history
  • Loading branch information
acwright committed Nov 11, 2019
2 parents 8540943 + bacc993 commit 3f9ed9a
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions Sources/Cabinet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public extension Cabinet {

let asynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { (asynchronousFetchResult) in
guard let result = asynchronousFetchResult.finalResult else {
completion(.success([]))
DispatchQueue.main.async { completion(.success([])) }
return
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public extension Cabinet {

let asynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { (asynchronousFetchResult) in
guard let result = asynchronousFetchResult.finalResult else {
completion(.success(false))
DispatchQueue.main.async { completion(.success(false)) }
return
}

Expand All @@ -126,39 +126,48 @@ public extension Cabinet {
func delete<I>(objects: [I], completion: @escaping (Result<Bool, Error>) -> Void) where I : Storable {
let managedObjectContext = self.persistentContainer.viewContext

let ids = objects.map { (object) -> UUID in
return object.id
}

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

let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
batchDeleteRequest.resultType = .resultTypeObjectIDs
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: I.entityName)
fetchRequest.predicate = NSPredicate(format: "id IN %@", objects.map { $0.id })

do {
let result = try managedObjectContext.execute(batchDeleteRequest) as! NSBatchDeleteResult
let changes: [AnyHashable: Any] = [
NSDeletedObjectsKey: result.result as! [NSManagedObjectID]
]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [managedObjectContext])
let asynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { (asynchronousFetchResult) in
guard let result = asynchronousFetchResult.finalResult else {
DispatchQueue.main.async { completion(.success(false)) }
return
}

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]
]

NSManagedObjectContext.mergeChanges(fromRemoteContextSave: childChanges, into: [managedObjectContext])
result.forEach { object in
I.cascades.forEach { cascade in
let childFetchRequest = NSFetchRequest<NSManagedObject>(entityName: cascade)
childFetchRequest.predicate = NSPredicate(format: "%K IN %@", I.foreignKey, objects.map { $0.id })

let childAsynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: childFetchRequest) { (childAsynchronousFetchRequest) in
guard let childResult = childAsynchronousFetchRequest.finalResult else {
DispatchQueue.main.async { completion(.success(false)) }
return
}

childResult.forEach { object in
managedObjectContext.delete(object)
}
}

do {
try managedObjectContext.execute(childAsynchronousFetchRequest)
} catch {
completion(.failure(error))
}
}

managedObjectContext.delete(object)
}

self.save(completion)
DispatchQueue.main.async {
self.save(completion)
}
}

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

0 comments on commit 3f9ed9a

Please sign in to comment.