From b220948e2b14bbb6ecf5122c6fa200a899311572 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 10 May 2020 14:54:43 -0400 Subject: [PATCH] Only initialize NSManagedObjectModel once Prevents CoreData warnings when switching accounts and constructing a second MastodonCachePersistentStore --- Tusker/CoreData/MastodonCachePersistentStore.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index 4807201d..b437deda 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -13,15 +13,18 @@ import Combine class MastodonCachePersistentStore: NSPersistentContainer { + private static let managedObjectModel: NSManagedObjectModel = { + let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")! + return NSManagedObjectModel(contentsOf: url)! + }() + private(set) lazy var backgroundContext = newBackgroundContext() let statusSubject = PassthroughSubject() let accountSubject = PassthroughSubject() init(for controller: MastodonController) { - let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")! - let model = NSManagedObjectModel(contentsOf: url)! - super.init(name: "\(controller.accountInfo!.id)_cache", managedObjectModel: model) + super.init(name: "\(controller.accountInfo!.id)_cache", managedObjectModel: MastodonCachePersistentStore.managedObjectModel) loadPersistentStores { (description, error) in if let error = error { fatalError("Unable to load persistent store: \(error)")