Don't parent background managed object contexts to view context
Otherwise, certain operations require the background contexts to interact with the view context, which can block the main thread from accessing the view context (potentially causing hitches if the view context access is in a critical path, like cell fetching).
This commit is contained in:
parent
66c17006d1
commit
8b78a5e7ad
|
@ -20,13 +20,15 @@ class MastodonCachePersistentStore: NSPersistentContainer {
|
|||
|
||||
private(set) lazy var backgroundContext: NSManagedObjectContext = {
|
||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||
context.parent = self.viewContext
|
||||
context.persistentStoreCoordinator = self.persistentStoreCoordinator
|
||||
context.automaticallyMergesChangesFromParent = true
|
||||
return context
|
||||
}()
|
||||
|
||||
private(set) lazy var prefetchBackgroundContext: NSManagedObjectContext = {
|
||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||
context.parent = self.viewContext
|
||||
context.persistentStoreCoordinator = self.persistentStoreCoordinator
|
||||
context.automaticallyMergesChangesFromParent = true
|
||||
return context
|
||||
}()
|
||||
|
||||
|
@ -51,6 +53,8 @@ class MastodonCachePersistentStore: NSPersistentContainer {
|
|||
}
|
||||
}
|
||||
|
||||
viewContext.automaticallyMergesChangesFromParent = true
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(managedObjectsDidChange), name: .NSManagedObjectContextObjectsDidChange, object: viewContext)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue