Reuse base fetch request building code
Set .predicate first for consistency.
This commit is contained in:
parent
ae6b5c50d9
commit
57f7b15aa2
|
@ -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 {
|
||||
|
|
|
@ -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<CDInfrastructure> {
|
||||
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 = [
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue