add in cloudkit toggle
This commit is contained in:
@@ -6,13 +6,10 @@
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import WidgetKit
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
class PersistenceController {
|
||||
private var dataUpdateCall: (() -> Void)?
|
||||
private let callDelay = 10
|
||||
@AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue) private var useCloudKit = false
|
||||
|
||||
static let shared = PersistenceController.persistenceController
|
||||
|
||||
@@ -24,6 +21,8 @@ class PersistenceController {
|
||||
return PersistenceController.shared.container.viewContext
|
||||
}
|
||||
|
||||
public var switchContainerListeners = [(() -> Void)]()
|
||||
|
||||
public func getEntry(byDate date: Date) -> MoodEntry? {
|
||||
let predicate = NSPredicate(format: "forDate == %@",
|
||||
date as NSDate)
|
||||
@@ -180,40 +179,49 @@ class PersistenceController {
|
||||
return entries
|
||||
}
|
||||
|
||||
let container: NSPersistentCloudKitContainer
|
||||
lazy var container: NSPersistentContainer = {
|
||||
setupContainer()
|
||||
}()
|
||||
|
||||
init(inMemory: Bool = false) {
|
||||
container = NSPersistentCloudKitContainer(name: "Feels")
|
||||
if inMemory {
|
||||
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
|
||||
func switchContainer() {
|
||||
try? viewContext.save()
|
||||
container = setupContainer()
|
||||
for item in switchContainerListeners {
|
||||
item()
|
||||
}
|
||||
}
|
||||
|
||||
private func setupContainer() -> NSPersistentContainer {
|
||||
if useCloudKit {
|
||||
container = NSPersistentCloudKitContainer(name: "Feels")
|
||||
} else {
|
||||
container = NSPersistentContainer(name: "Feels")
|
||||
}
|
||||
|
||||
guard let description = container.persistentStoreDescriptions.first else {
|
||||
fatalError("Could not retrieve a persistent store description.")
|
||||
}
|
||||
|
||||
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
||||
if let error = error as NSError? {
|
||||
// Replace this implementation with code to handle the error appropriately.
|
||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||
|
||||
/*
|
||||
Typical reasons for an error here include:
|
||||
* The parent directory does not exist, cannot be created, or disallows writing.
|
||||
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
|
||||
* The device is out of space.
|
||||
* The store could not be migrated to the current model version.
|
||||
Check the error message to determine what the actual problem was.
|
||||
*/
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
})
|
||||
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
|
||||
container.viewContext.automaticallyMergesChangesFromParent = true
|
||||
|
||||
for description in container.persistentStoreDescriptions {
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
|
||||
}
|
||||
|
||||
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
||||
if let error = error as NSError? {
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
})
|
||||
|
||||
return container
|
||||
}
|
||||
|
||||
init(inMemory: Bool = false) {
|
||||
container = setupContainer()
|
||||
}
|
||||
|
||||
|
||||
public func splitIntoYearMonth() -> [Int: [Int: [MoodEntry]]] {
|
||||
let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0),
|
||||
|
||||
Reference in New Issue
Block a user