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 {
|
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||||
// PersistenceController.shared.clearDB()
|
// PersistenceController.shared.clearDB()
|
||||||
|
PersistenceController.shared.removeNoForDates()
|
||||||
PersistenceController.shared.fillInMissingDates()
|
PersistenceController.shared.fillInMissingDates()
|
||||||
UNUserNotificationCenter.current().delegate = self
|
UNUserNotificationCenter.current().delegate = self
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class PersistenceController {
|
|||||||
|
|
||||||
static let shared = PersistenceController.persistenceController
|
static let shared = PersistenceController.persistenceController
|
||||||
|
|
||||||
|
public var listeners = [(() -> Void)]()
|
||||||
|
|
||||||
private static var persistenceController: PersistenceController {
|
private static var persistenceController: PersistenceController {
|
||||||
return PersistenceController(inMemory: false)
|
return PersistenceController(inMemory: false)
|
||||||
}
|
}
|
||||||
@@ -77,6 +79,12 @@ class PersistenceController {
|
|||||||
init(inMemory: Bool = false) {
|
init(inMemory: Bool = false) {
|
||||||
container = setupContainer()
|
container = setupContainer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateListeners() {
|
||||||
|
for listener in listeners {
|
||||||
|
listener()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NSManagedObjectContext {
|
extension NSManagedObjectContext {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ extension PersistenceController {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
try viewContext.save()
|
try viewContext.save()
|
||||||
|
updateListeners()
|
||||||
} catch {
|
} catch {
|
||||||
let nsError = error as NSError
|
let nsError = error as NSError
|
||||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
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 {
|
do {
|
||||||
try viewContext.executeAndMergeChanges(using: deleteRequest)
|
try viewContext.executeAndMergeChanges(using: deleteRequest)
|
||||||
try viewContext.save()
|
try viewContext.save()
|
||||||
|
updateListeners()
|
||||||
} catch let error as NSError {
|
} catch let error as NSError {
|
||||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||||
}
|
}
|
||||||
@@ -24,6 +25,7 @@ extension PersistenceController {
|
|||||||
if let entry = PersistenceController.shared.getEntry(byDate: forDate) {
|
if let entry = PersistenceController.shared.getEntry(byDate: forDate) {
|
||||||
viewContext.delete(entry)
|
viewContext.delete(entry)
|
||||||
try! viewContext.save()
|
try! viewContext.save()
|
||||||
|
updateListeners()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,25 +10,12 @@ import Foundation
|
|||||||
extension PersistenceController {
|
extension PersistenceController {
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func update(entryDate: Date, withModd mood: Mood) -> Bool {
|
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 {
|
do {
|
||||||
|
if let entry = PersistenceController.shared.getEntry(byDate: entryDate) {
|
||||||
|
viewContext.delete(entry)
|
||||||
|
}
|
||||||
|
|
||||||
try viewContext.save()
|
try viewContext.save()
|
||||||
return true
|
|
||||||
} catch {
|
} catch {
|
||||||
// Replace this implementation with code to handle the error appropriately.
|
// 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.
|
// 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)")
|
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||||
return false
|
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,
|
deleteEnabled,
|
||||||
selectedEntry.mood != .missing {
|
selectedEntry.mood != .missing {
|
||||||
Button(String(localized: "content_view_delete_entry"), action: {
|
Button(String(localized: "content_view_delete_entry"), action: {
|
||||||
updateEntries()
|
|
||||||
PersistenceController.shared.update(entryDate: selectedEntry.forDate!, withModd: .missing)
|
PersistenceController.shared.update(entryDate: selectedEntry.forDate!, withModd: .missing)
|
||||||
|
updateEntries()
|
||||||
showUpdateEntryAlert = false
|
showUpdateEntryAlert = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ class HomeViewViewModel: ObservableObject {
|
|||||||
self.getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
|
self.getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
PersistenceController.shared.listeners.append { [weak self] in
|
||||||
|
self?.updateData()
|
||||||
|
}
|
||||||
updateData()
|
updateData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user