Prevent crashing when DataStack is deallocated ahead of ListMonitor and child objects

This commit is contained in:
John Estropia
2019-06-10 18:34:15 +09:00
parent 30ab4386bf
commit 9f397b4337
2 changed files with 19 additions and 4 deletions

View File

@@ -140,8 +140,16 @@ public struct From<D: DynamicObject> {
}
internal func applyToFetchRequest<U>(_ fetchRequest: CoreStoreFetchRequest<U>, context: NSManagedObjectContext, applyAffectedStores: Bool = true) throws {
fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!
guard let parentStack = context.parentStack else {
CoreStore.log(
.warning,
message: "Attempted to perform a fetch but the \(cs_typeName(DataStack.self)) has already been deallocated."
)
throw CoreStoreError.unknown
}
fetchRequest.entity = parentStack.entityDescription(for: EntityIdentifier(self.entityClass))!
guard applyAffectedStores else {
return

View File

@@ -963,8 +963,15 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
return
}
try! newFetchedResultsController.performFetchFromSpecifiedStores()
do {
try newFetchedResultsController.performFetchFromSpecifiedStores()
}
catch {
// DataStack may have been deallocated
return
}
self.fetchedResultsControllerDelegate.taskGroup.notify(queue: .main) {
self.fetchedResultsControllerDelegate.enabled = false