chore: Address PR codestyle feedback

This commit is contained in:
Marcus Kida 2022-12-14 09:51:15 +01:00
parent 855f626c42
commit f530d109d3
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
2 changed files with 41 additions and 27 deletions

View File

@ -6,17 +6,33 @@ import MastodonSDK
extension APIService.CoreData {
static func createOrMergeInstanceV2(
into managedObjectContext: NSManagedObjectContext,
domain: String,
entity: Mastodon.Entity.V2.Instance,
networkDate: Date,
log: Logger
public struct PersistContext {
public let domain: String
public let entity: Mastodon.Entity.V2.Instance
public let networkDate: Date
public let log: Logger
public init(
domain: String,
entity: Mastodon.Entity.V2.Instance,
networkDate: Date,
log: Logger
) {
self.domain = domain
self.entity = entity
self.networkDate = networkDate
self.log = log
}
}
static func createOrMergeInstance(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> (instance: Instance, isCreated: Bool) {
// fetch old mastodon user
let old: Instance? = {
let request = Instance.sortedFetchRequest
request.predicate = Instance.predicate(domain: domain)
request.predicate = Instance.predicate(domain: context.domain)
request.fetchLimit = 1
request.returnsObjectsAsFaults = false
do {
@ -28,19 +44,17 @@ extension APIService.CoreData {
}()
if let old = old {
APIService.CoreData.mergeV2(
APIService.CoreData.merge(
instance: old,
entity: entity,
domain: domain,
networkDate: networkDate
context: context
)
return (old, false)
} else {
let instance = Instance.insert(
into: managedObjectContext,
property: Instance.Property(domain: domain, version: entity.version)
property: Instance.Property(domain: context.domain, version: context.entity.version)
)
let configurationRaw = entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
let configurationRaw = context.entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
instance.update(configurationV2Raw: configurationRaw)
return (instance, true)
@ -51,19 +65,17 @@ extension APIService.CoreData {
extension APIService.CoreData {
static func mergeV2(
static func merge(
instance: Instance,
entity: Mastodon.Entity.V2.Instance,
domain: String,
networkDate: Date
context: PersistContext
) {
guard networkDate > instance.updatedAt else { return }
guard context.networkDate > instance.updatedAt else { return }
let configurationRaw = entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
let configurationRaw = context.entity.configuration.flatMap { Instance.encodeV2(configuration: $0) }
instance.update(configurationV2Raw: configurationRaw)
instance.version = entity.version
instance.version = context.entity.version
instance.didUpdate(at: networkDate)
instance.didUpdate(at: context.networkDate)
}
}

View File

@ -118,12 +118,14 @@ extension InstanceService {
let managedObjectContext = self.backgroundManagedObjectContext
return managedObjectContext.performChanges {
// get instance
let (instance, _) = APIService.CoreData.createOrMergeInstanceV2(
into: managedObjectContext,
domain: domain,
entity: response.value,
networkDate: response.networkDate,
log: Logger(subsystem: "Update", category: "InstanceService")
let (instance, _) = APIService.CoreData.createOrMergeInstance(
in: managedObjectContext,
context: .init(
domain: domain,
entity: response.value,
networkDate: response.networkDate,
log: Logger(subsystem: "Update", category: "InstanceService")
)
)
// update relationship