Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW-201 Fix CoreData Persistence #170

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions SmartWeights/SmartWeights/DBAccess/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import Foundation
import CoreData

class CoreDataManager: ObservableObject {

let persistentContainer: NSPersistentCloudKitContainer
// Use the shared managed object model defined in PersistenceController
let persistentContainer: NSPersistentCloudKitContainer = PersistenceController.shared.container

init() {
// Use the shared managed object model defined in PersistenceController
persistentContainer = NSPersistentCloudKitContainer(name: "SmartWeights", managedObjectModel: PersistenceController.sharedManagedObjectModel)

let description = NSPersistentStoreDescription()
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
Expand Down
19 changes: 14 additions & 5 deletions SmartWeights/SmartWeights/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ struct PersistenceController {
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "SmartWeights", managedObjectModel: PersistenceController.sharedManagedObjectModel)

// Configures the persistent store for in-memory use if specified. This is useful for unit tests or preview functionality.
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}

// Loads the persistent stores and handles any errors.
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
Expand All @@ -76,3 +71,17 @@ struct PersistenceController {
container.viewContext.automaticallyMergesChangesFromParent = true
}
}

extension PersistenceController {
func saveContext() {
let context = container.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
31 changes: 31 additions & 0 deletions SmartWeights/SmartWeights/SmartWeightsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,40 @@
//

import SwiftUI
import CoreData

// AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
saveContext()
}

func applicationWillTerminate(_ application: UIApplication) {
saveContext()
}

func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

lazy var persistentContainer: NSPersistentContainer = PersistenceController.shared.container
}

@main
struct SmartWeightsApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
Expand Down