diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift index ef5988b4..ca2154c7 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift @@ -65,13 +65,13 @@ class ProfileRepository: Repository { func profile(withId id: UUID) -> Profile? { let request = CDProfile.fetchRequest() - request.sortDescriptors = [ - .init(keyPath: \CDProfile.lastUpdate, ascending: false) - ] request.predicate = NSPredicate( format: "uuid == %@", id.uuidString ) + request.sortDescriptors = [ + .init(keyPath: \CDProfile.lastUpdate, ascending: false) + ] do { let results = try context.fetch(request) guard let recent = results.first else { diff --git a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/InfrastructureRepository.swift b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/InfrastructureRepository.swift index 14780b1e..92f72612 100644 --- a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/InfrastructureRepository.swift +++ b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/InfrastructureRepository.swift @@ -49,12 +49,7 @@ class InfrastructureRepository: Repository { return provider }() - let request = CDInfrastructure.fetchRequest() - request.predicate = NSPredicate( - format: "provider.name == %@ AND vpnProtocol == %@", - infrastructure.name, - vpnProtocol.rawValue - ) + let request = fetchRequest(infrastructure.name, vpnProtocol) let existing = try context.fetch(request) existing.forEach(context.delete) @@ -75,15 +70,10 @@ class InfrastructureRepository: Repository { } func defaultUsername(forProviderWithName name: ProviderName, vpnProtocol: VPNProtocolType) -> String? { - let request = CDInfrastructure.fetchRequest() + let request = fetchRequest(name, vpnProtocol) request.sortDescriptors = [ .init(keyPath: \CDInfrastructure.lastUpdate, ascending: false) ] - request.predicate = NSPredicate( - format: "provider.name == %@ AND vpnProtocol == %@", - name, - vpnProtocol.rawValue - ) request.relationshipKeyPathsForPrefetching = [ "defaults" ] @@ -99,19 +89,11 @@ class InfrastructureRepository: Repository { } } - func lastInfrastructureUpdate( - withName name: ProviderName, - vpnProtocol: VPNProtocolType - ) -> Date? { - let request = CDInfrastructure.fetchRequest() + func lastInfrastructureUpdate(withName name: ProviderName, vpnProtocol: VPNProtocolType) -> Date? { + let request = fetchRequest(name, vpnProtocol) request.sortDescriptors = [ .init(keyPath: \CDInfrastructure.lastUpdate, ascending: false) ] - request.predicate = NSPredicate( - format: "provider.name == %@ AND vpnProtocol == %@", - name, - vpnProtocol.rawValue - ) request.relationshipKeyPathsForPrefetching = [ "provider", "provider.infrastructures" @@ -131,6 +113,16 @@ class InfrastructureRepository: Repository { } } + private func fetchRequest(_ name: ProviderName, _ vpnProtocol: VPNProtocolType) -> NSFetchRequest { + let request = CDInfrastructure.fetchRequest() + request.predicate = NSPredicate( + format: "provider.name == %@ AND vpnProtocol == %@", + name, + vpnProtocol.rawValue + ) + return request + } + private func providerDTO(forName name: ProviderName) throws -> CDProvider? { let request = CDProvider.fetchRequest() request.sortDescriptors = [ diff --git a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ProviderRepository.swift b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ProviderRepository.swift index 3fb7249d..34988ef9 100644 --- a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ProviderRepository.swift +++ b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ProviderRepository.swift @@ -58,10 +58,10 @@ class ProviderRepository: Repository { func provider(withName name: ProviderName) -> ProviderMetadata? { let request = CDProvider.fetchRequest() + request.predicate = NSPredicate(format: "name == %@", name) request.sortDescriptors = [ .init(keyPath: \CDProvider.lastUpdate, ascending: false) ] - request.predicate = NSPredicate(format: "name == %@", name) request.relationshipKeyPathsForPrefetching = [ "infrastructures" ] diff --git a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ServerRepository.swift b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ServerRepository.swift index d2762e38..8c679dae 100644 --- a/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ServerRepository.swift +++ b/PassepartoutCore/Sources/PassepartoutProviders/Repositories/ServerRepository.swift @@ -139,9 +139,8 @@ class ServerRepository: Repository { func anyDefaultServer(forProviderWithName providerName: ProviderName, vpnProtocol: VPNProtocolType) -> ProviderServer? { let request = CDInfrastructureServer.fetchRequest() - let format = "countryCode == category.infrastructure.defaults.countryCode AND category.infrastructure.provider.name == %@ AND category.infrastructure.vpnProtocol == %@" request.predicate = NSPredicate( - format: format, + format: "countryCode == category.infrastructure.defaults.countryCode AND category.infrastructure.provider.name == %@ AND category.infrastructure.vpnProtocol == %@", providerName, vpnProtocol.rawValue )