From 9b9191b6b1de1a87e5be905ad62a336d2dd644a3 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 27 May 2022 08:42:34 -0700 Subject: [PATCH] clear core data when switch env --- .../Services/DataService/DataService.swift | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 6878a40dc..5c458ea3c 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -59,9 +59,8 @@ public final class DataService: ObservableObject { await networker.hasConnectionAndValidToken() } - private func resetCoreData() { - let storeContainer = - persistentContainer.persistentStoreCoordinator + private func clearCoreData() { + let storeContainer = persistentContainer.persistentStoreCoordinator do { for store in storeContainer.persistentStores { @@ -71,18 +70,23 @@ public final class DataService: ObservableObject { options: nil ) } - persistentContainer = PersistentContainer.make() - persistentContainer.loadPersistentStores { _, error in - if let error = error { - fatalError("Core Data store failed to load with error: \(error)") - } - } - backgroundContext = persistentContainer.newBackgroundContext() } catch { - logger.debug("Failed to reset core data stores") + logger.debug("Failed to clear core data stores") } } + private func resetCoreData() { + clearCoreData() + + persistentContainer = PersistentContainer.make() + persistentContainer.loadPersistentStores { _, error in + if let error = error { + fatalError("Core Data store failed to load with error: \(error)") + } + } + backgroundContext = persistentContainer.newBackgroundContext() + } + private func isFirstTimeRunningNewAppVersion() -> Bool { let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") guard let appVersion = appVersion as? String else { return false }