Add rollback for failed dictionary operations

This commit is contained in:
Beingpax 2025-12-28 12:32:14 +05:45
parent 7beb63e3c6
commit bf3c035e58
2 changed files with 8 additions and 0 deletions

View File

@ -158,6 +158,8 @@ struct VocabularyView: View {
do {
try modelContext.save()
} catch {
// Rollback the insert to maintain UI consistency
modelContext.delete(newWord)
alertMessage = "Failed to add word: \(error.localizedDescription)"
showAlert = true
}
@ -169,6 +171,8 @@ struct VocabularyView: View {
do {
try modelContext.save()
} catch {
// Rollback the delete to restore UI consistency
modelContext.rollback()
alertMessage = "Failed to remove word: \(error.localizedDescription)"
showAlert = true
}

View File

@ -227,6 +227,8 @@ struct WordReplacementView: View {
originalWord = ""
replacementWord = ""
} catch {
// Rollback the insert to maintain UI consistency
modelContext.delete(newReplacement)
alertMessage = "Failed to add replacement: \(error.localizedDescription)"
showAlert = true
}
@ -238,6 +240,8 @@ struct WordReplacementView: View {
do {
try modelContext.save()
} catch {
// Rollback the delete to restore UI consistency
modelContext.rollback()
alertMessage = "Failed to remove replacement: \(error.localizedDescription)"
showAlert = true
}