fix crash when updating an existing entry
code clean up
This commit is contained in:
@@ -59,12 +59,18 @@ class PersistenceController {
|
|||||||
editedDataClosure.append(closure)
|
editedDataClosure.append(closure)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func runDataListeners() {
|
public func saveAndRunDataListerners() {
|
||||||
for closure in editedDataClosure {
|
do {
|
||||||
closure()
|
try viewContext.save()
|
||||||
|
|
||||||
|
for closure in editedDataClosure {
|
||||||
|
closure()
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupContainer() -> NSPersistentContainer {
|
private func setupContainer() -> NSPersistentContainer {
|
||||||
if useCloudKit {
|
if useCloudKit {
|
||||||
container = NSPersistentCloudKitContainer(name: "Feels")
|
container = NSPersistentCloudKitContainer(name: "Feels")
|
||||||
|
|||||||
@@ -23,13 +23,7 @@ extension PersistenceController {
|
|||||||
newItem.canDelete = true
|
newItem.canDelete = true
|
||||||
newItem.entryType = Int16(entryType.rawValue)
|
newItem.entryType = Int16(entryType.rawValue)
|
||||||
|
|
||||||
do {
|
saveAndRunDataListerners()
|
||||||
try viewContext.save()
|
|
||||||
runDataListeners()
|
|
||||||
} catch {
|
|
||||||
let nsError = error as NSError
|
|
||||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillInMissingDates() {
|
func fillInMissingDates() {
|
||||||
|
|||||||
@@ -14,18 +14,9 @@ extension PersistenceController {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
try viewContext.executeAndMergeChanges(using: deleteRequest)
|
try viewContext.executeAndMergeChanges(using: deleteRequest)
|
||||||
try viewContext.save()
|
saveAndRunDataListerners()
|
||||||
runDataListeners()
|
|
||||||
} catch let error as NSError {
|
} catch let error as NSError {
|
||||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func delete(forDate: Date) {
|
|
||||||
if let entry = PersistenceController.shared.getEntry(byDate: forDate) {
|
|
||||||
viewContext.delete(entry)
|
|
||||||
try! viewContext.save()
|
|
||||||
runDataListeners()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ extension PersistenceController {
|
|||||||
let items = PersistenceController.shared.getData(startDate: startDateOfMonth,
|
let items = PersistenceController.shared.getData(startDate: startDateOfMonth,
|
||||||
endDate: startDateOfMonth.endOfMonth,
|
endDate: startDateOfMonth.endOfMonth,
|
||||||
includedDays: [1,2,3,4,5,6,7])
|
includedDays: [1,2,3,4,5,6,7])
|
||||||
|
|
||||||
if !items.isEmpty {
|
if !items.isEmpty {
|
||||||
allMonths[month] = items
|
allMonths[month] = items
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,8 +75,7 @@ extension PersistenceController {
|
|||||||
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
|
newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date))
|
||||||
}
|
}
|
||||||
|
|
||||||
try viewContext.save()
|
saveAndRunDataListerners()
|
||||||
runDataListeners()
|
|
||||||
} 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.
|
||||||
|
|||||||
@@ -10,34 +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 {
|
||||||
do {
|
guard let existingEntry = getEntry(byDate: entryDate) else {
|
||||||
if let entry = PersistenceController.shared.getEntry(byDate: entryDate) {
|
|
||||||
viewContext.delete(entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
existingEntry.setValue(mood.rawValue, forKey: "moodValue")
|
||||||
|
saveAndRunDataListerners()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
runDataListeners()
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,12 +52,11 @@ class HomeViewViewModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func getGroupedData(addMonthStartWeekdayPadding: Bool) {
|
private func getGroupedData(addMonthStartWeekdayPadding: Bool) {
|
||||||
grouped = PersistenceController.shared.splitIntoYearMonth()
|
var newStuff = PersistenceController.shared.splitIntoYearMonth()
|
||||||
|
|
||||||
if addMonthStartWeekdayPadding {
|
if addMonthStartWeekdayPadding {
|
||||||
grouped = MoodEntryFunctions.padMoodEntriesForCalendar(entries: grouped)
|
newStuff = MoodEntryFunctions.padMoodEntriesForCalendar(entries: grouped)
|
||||||
}
|
}
|
||||||
//
|
grouped = newStuff
|
||||||
numberOfItems = numberOfEntries
|
numberOfItems = numberOfEntries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user