A couple of users have been getting these odd crashes that I can't reproduce on my end. This happens the first time they try to create anything in my app, then never again. But it happens once for each type of entity. Here's an excerpt of the crash log:
Frame 5 of the last exception backtrace is the user pushing the save button, which takes a generic view model that conforms to the EditorViewModel protocol, like so:
The next frame is the save method on the view model which saves the changes made to the assignment entity in Core Data:
I'm a pretty new developer, and this app is a pet project of mine so I'm really struggling to figure out what the issue could be, especially since I can't reproduce it on my end. Any suggestions would be much appreciated!
Frame 5 of the last exception backtrace is the user pushing the save button, which takes a generic view model that conforms to the EditorViewModel protocol, like so:
Code Block Swift struct AddSaveNavigationBarItem<T: EditorViewModel>: View { @ObservedObject var viewModel: T @Binding var presentationMode: PresentationMode var body: some View { Button(saveAddButtonTitle) { if let assignmentEditorViewModel = viewModel as? AssignmentEditorViewModel { guard !assignmentEditorViewModel.shouldWarnDenominator() else { return } guard !assignmentEditorViewModel.shouldWarnDisableGrades() else { return } } viewModel.save() presentationMode.dismiss() } .disabled(viewModel.name.isEmpty) } }
The next frame is the save method on the view model which saves the changes made to the assignment entity in Core Data:
Code Block Swift func save() { assignment.name = name assignment.course = course assignment.isTest = isTest assignment.isDueOnDate = isDueOnDate assignment.isDueAtTime = isDueAtTime if !isDueAtTime && isDueOnDate { assignment.dueDate = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: dueDate) } else { assignment.dueDate = dueDate } assignment.notes = notes if willRecieveGrade { assignment.gradeNumerator = Double(gradeNumerator) ?? Assignment.defaultNumerator assignment.gradeDenominator = Double(gradeDenominator) ?? Assignment.defaultDenominator if !isUsingCustomWeight && weightCategory != nil { assignment.weight = Assignment.defaultWeight assignment.weightCategory = weightCategory! } else { assignment.weight = Double(weight) ?? Assignment.defaultWeight assignment.weightCategory = nil } } else { assignment.gradeNumerator = Assignment.defaultNumerator assignment.gradeDenominator = Assignment.defaultDenominator assignment.weight = Assignment.defaultWeight assignment.weightCategory = nil } assignment.willRecieveGrade = willRecieveGrade do { try context.save() AssignmentEditorViewModel.logger.info("Saved changes to assignment \(self.assignment.id, privacy: .public)") } catch { AssignmentEditorViewModel.logger.error("Failed to save context: \(error.localizedDescription, privacy: .public)") } for subtask in assignment.subtasks { subtask.objectWillChange.send() } assignment.objectWillChange.send() }
I'm a pretty new developer, and this app is a pet project of mine so I'm really struggling to figure out what the issue could be, especially since I can't reproduce it on my end. Any suggestions would be much appreciated!