From 8b78a5e7adc2731622bbf5f03212945ca42acd89 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 11 Sep 2022 23:00:51 -0400 Subject: [PATCH] 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). --- Tusker/CoreData/MastodonCachePersistentStore.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index cb934877ed..22150e96fd 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -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) }