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
committed by Magesh K
parent bd3beb5983
commit f884d72a8b
3 changed files with 43 additions and 6 deletions

View File

@@ -283,13 +283,31 @@ private extension SourcesViewController
AppManager.shared.fetchSource(sourceURL: url, dependencies: dependencies) { (result) in
do
{
// Use @Managed before calling perform() to keep
// strong reference to source.managedObjectContext.
@Managed var source = try result.get()
DispatchQueue.main.async {
self.showSourceDetails(for: source)
let backgroundContext = DatabaseManager.shared.persistentContainer.newBackgroundContext()
backgroundContext.perform {
do
{
let predicate = NSPredicate(format: "%K == %@", #keyPath(Source.identifier), $source.identifier)
if let existingSource = Source.first(satisfying: predicate, in: backgroundContext)
{
throw SourceError.duplicate(source, previousSourceName: existingSource.name)
}
DispatchQueue.main.async {
self.showSourceDetails(for: source)
}
finish(.success(()))
}
catch
{
finish(.failure(error))
}
}
finish(.success(()))
}
catch
{