Verifies source’s identifier doesn’t match existing sources when adding

This commit is contained in:
Riley Testut
2023-05-11 18:51:09 -05:00
parent 465be339fd
commit 729eb89433
3 changed files with 43 additions and 6 deletions

View File

@@ -353,8 +353,12 @@ extension AppManager
let action = await UIAlertAction(title: NSLocalizedString("Add Source", comment: ""), style: .default)
try await presentingViewController.presentConfirmationAlert(title: title, message: message, primaryAction: action)
// Wait for fetch to finish before saving context.
_ = try await fetchedSource
// Wait for fetch to finish before saving context to make
// sure there isn't already a source with this identifier.
let sourceExists = try await fetchedSource.isAdded
// This is just a sanity check, so pass nil for previousSourceName to keep code simple.
guard !sourceExists else { throw SourceError.duplicate(source, previousSourceName: nil) }
try await context.performAsync {
try context.save()