fix crash where homeViewTwo view model wasnt being updated
This commit is contained in:
@@ -16,6 +16,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
||||
|
||||
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||
// PersistenceController.shared.clearDB()
|
||||
PersistenceController.shared.removeNoForDates()
|
||||
PersistenceController.shared.fillInMissingDates()
|
||||
UNUserNotificationCenter.current().delegate = self
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ class PersistenceController {
|
||||
|
||||
static let shared = PersistenceController.persistenceController
|
||||
|
||||
public var listeners = [(() -> Void)]()
|
||||
|
||||
private static var persistenceController: PersistenceController {
|
||||
return PersistenceController(inMemory: false)
|
||||
}
|
||||
@@ -77,6 +79,12 @@ class PersistenceController {
|
||||
init(inMemory: Bool = false) {
|
||||
container = setupContainer()
|
||||
}
|
||||
|
||||
func updateListeners() {
|
||||
for listener in listeners {
|
||||
listener()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension NSManagedObjectContext {
|
||||
|
||||
@@ -25,6 +25,7 @@ extension PersistenceController {
|
||||
|
||||
do {
|
||||
try viewContext.save()
|
||||
updateListeners()
|
||||
} catch {
|
||||
let nsError = error as NSError
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
@@ -64,4 +65,18 @@ extension PersistenceController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeNoForDates() {
|
||||
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
|
||||
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "forDate", ascending: false)]
|
||||
let entries = try! viewContext.fetch(fetchRequest)
|
||||
|
||||
for entry in entries {
|
||||
guard let _ = entry.forDate else {
|
||||
viewContext.delete(entry)
|
||||
try? viewContext.save()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ extension PersistenceController {
|
||||
do {
|
||||
try viewContext.executeAndMergeChanges(using: deleteRequest)
|
||||
try viewContext.save()
|
||||
updateListeners()
|
||||
} catch let error as NSError {
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
@@ -24,6 +25,7 @@ extension PersistenceController {
|
||||
if let entry = PersistenceController.shared.getEntry(byDate: forDate) {
|
||||
viewContext.delete(entry)
|
||||
try! viewContext.save()
|
||||
updateListeners()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,25 +10,12 @@ import Foundation
|
||||
extension PersistenceController {
|
||||
@discardableResult
|
||||
public func update(entryDate: Date, withModd mood: Mood) -> Bool {
|
||||
if let entry = PersistenceController.shared.getEntry(byDate: entryDate) {
|
||||
viewContext.delete(entry)
|
||||
}
|
||||
|
||||
do {
|
||||
try PersistenceController.shared.viewContext.save()
|
||||
} catch {
|
||||
// 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.
|
||||
let nsError = error as NSError
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
return false
|
||||
}
|
||||
|
||||
add(mood: mood, forDate: entryDate, entryType: .listView)
|
||||
|
||||
do {
|
||||
if let entry = PersistenceController.shared.getEntry(byDate: entryDate) {
|
||||
viewContext.delete(entry)
|
||||
}
|
||||
|
||||
try viewContext.save()
|
||||
return true
|
||||
} catch {
|
||||
// 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.
|
||||
@@ -36,5 +23,21 @@ extension PersistenceController {
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
do {
|
||||
add(mood: mood, forDate: entryDate, entryType: .listView)
|
||||
try viewContext.save()
|
||||
} catch {
|
||||
// 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.
|
||||
let nsError = error as NSError
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
return false
|
||||
}
|
||||
|
||||
updateListeners()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +72,8 @@ struct MonthDetailView: View {
|
||||
deleteEnabled,
|
||||
selectedEntry.mood != .missing {
|
||||
Button(String(localized: "content_view_delete_entry"), action: {
|
||||
updateEntries()
|
||||
PersistenceController.shared.update(entryDate: selectedEntry.forDate!, withModd: .missing)
|
||||
updateEntries()
|
||||
showUpdateEntryAlert = false
|
||||
})
|
||||
}
|
||||
|
||||
@@ -43,6 +43,9 @@ class HomeViewViewModel: ObservableObject {
|
||||
self.getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
|
||||
|
||||
}
|
||||
PersistenceController.shared.listeners.append { [weak self] in
|
||||
self?.updateData()
|
||||
}
|
||||
updateData()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user