Remove persistence for tags (IOS-192)
Was connected to `let me = authenticationBox.authentication.user(in: managedObjectContext)`
This commit is contained in:
parent
77f0f28960
commit
bb3ad77954
|
@ -1,135 +0,0 @@
|
|||
//
|
||||
// Persistence+Tag.swift
|
||||
// Mastodon
|
||||
//
|
||||
// Created by MainasuK on 2022-1-20.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import CoreDataStack
|
||||
import Foundation
|
||||
import MastodonSDK
|
||||
|
||||
extension Persistence.Tag {
|
||||
|
||||
public struct PersistContext {
|
||||
public let domain: String
|
||||
public let entity: Mastodon.Entity.Tag
|
||||
public let me: MastodonUser?
|
||||
public let networkDate: Date
|
||||
|
||||
public init(
|
||||
domain: String,
|
||||
entity: Mastodon.Entity.Tag,
|
||||
me: MastodonUser?,
|
||||
networkDate: Date
|
||||
) {
|
||||
self.domain = domain
|
||||
self.entity = entity
|
||||
self.me = me
|
||||
self.networkDate = networkDate
|
||||
}
|
||||
}
|
||||
|
||||
public struct PersistResult {
|
||||
public let tag: Tag
|
||||
public let isNewInsertion: Bool
|
||||
|
||||
public init(
|
||||
tag: Tag,
|
||||
isNewInsertion: Bool
|
||||
) {
|
||||
self.tag = tag
|
||||
self.isNewInsertion = isNewInsertion
|
||||
}
|
||||
}
|
||||
|
||||
public static func createOrMerge(
|
||||
in managedObjectContext: NSManagedObjectContext,
|
||||
context: PersistContext
|
||||
) -> PersistResult {
|
||||
if let old = fetch(in: managedObjectContext, context: context) {
|
||||
merge(tag: old, context: context)
|
||||
return PersistResult(
|
||||
tag: old,
|
||||
isNewInsertion: false
|
||||
)
|
||||
} else {
|
||||
let object = create(
|
||||
in: managedObjectContext,
|
||||
context: context
|
||||
)
|
||||
|
||||
return PersistResult(
|
||||
tag: object,
|
||||
isNewInsertion: false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Persistence.Tag {
|
||||
|
||||
public static func fetch(
|
||||
in managedObjectContext: NSManagedObjectContext,
|
||||
context: PersistContext
|
||||
) -> Tag? {
|
||||
let request = Tag.sortedFetchRequest
|
||||
request.predicate = Tag.predicate(domain: context.domain, name: context.entity.name)
|
||||
request.fetchLimit = 1
|
||||
do {
|
||||
return try managedObjectContext.fetch(request).first
|
||||
} catch {
|
||||
assertionFailure(error.localizedDescription)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public static func create(
|
||||
in managedObjectContext: NSManagedObjectContext,
|
||||
context: PersistContext
|
||||
) -> Tag {
|
||||
let property = Tag.Property(
|
||||
entity: context.entity,
|
||||
domain: context.domain,
|
||||
networkDate: context.networkDate
|
||||
)
|
||||
let object = Tag.insert(
|
||||
into: managedObjectContext,
|
||||
property: property
|
||||
)
|
||||
update(tag: object, context: context)
|
||||
if let followingUser = context.me {
|
||||
object.update(followed: property.following, by: followingUser)
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
public static func merge(
|
||||
tag: Tag,
|
||||
context: PersistContext
|
||||
) {
|
||||
guard context.networkDate > tag.updatedAt else { return }
|
||||
let property = Tag.Property(
|
||||
entity: context.entity,
|
||||
domain: context.domain,
|
||||
networkDate: context.networkDate
|
||||
)
|
||||
|
||||
tag.update(property: property)
|
||||
if let followingUser = context.me {
|
||||
tag.update(followed: property.following, by: followingUser)
|
||||
}
|
||||
update(tag: tag, context: context)
|
||||
}
|
||||
|
||||
private static func update(
|
||||
tag: Tag,
|
||||
context: PersistContext
|
||||
) {
|
||||
tag.update(updatedAt: context.networkDate)
|
||||
}
|
||||
|
||||
}
|
|
@ -48,7 +48,6 @@ extension Persistence {
|
|||
public enum Poll { }
|
||||
public enum Card { }
|
||||
public enum PollOption { }
|
||||
public enum Tag { }
|
||||
public enum SearchHistory { }
|
||||
public enum Notification { }
|
||||
}
|
||||
|
|
|
@ -27,8 +27,8 @@ extension APIService {
|
|||
authorization: authorization
|
||||
).singleOutput()
|
||||
|
||||
return try await persistTag(from: response, domain: domain, authenticationBox: authenticationBox)
|
||||
} // end func
|
||||
return response
|
||||
}
|
||||
|
||||
public func followTag(
|
||||
for tag: String,
|
||||
|
@ -44,8 +44,8 @@ extension APIService {
|
|||
authorization: authorization
|
||||
).singleOutput()
|
||||
|
||||
return try await persistTag(from: response, domain: domain, authenticationBox: authenticationBox)
|
||||
} // end func
|
||||
return response
|
||||
}
|
||||
|
||||
public func unfollowTag(
|
||||
for tag: String,
|
||||
|
@ -61,32 +61,6 @@ extension APIService {
|
|||
authorization: authorization
|
||||
).singleOutput()
|
||||
|
||||
return try await persistTag(from: response, domain: domain, authenticationBox: authenticationBox)
|
||||
} // end func
|
||||
}
|
||||
|
||||
fileprivate extension APIService {
|
||||
@available(*, deprecated, message: "We don't persist tags anymore")
|
||||
func persistTag(
|
||||
from response: Mastodon.Response.Content<Mastodon.Entity.Tag>,
|
||||
domain: String,
|
||||
authenticationBox: MastodonAuthenticationBox
|
||||
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Tag> {
|
||||
let managedObjectContext = self.backgroundManagedObjectContext
|
||||
try await managedObjectContext.performChanges {
|
||||
let me = authenticationBox.authentication.user(in: managedObjectContext)
|
||||
|
||||
_ = Persistence.Tag.createOrMerge(
|
||||
in: managedObjectContext,
|
||||
context: Persistence.Tag.PersistContext(
|
||||
domain: domain,
|
||||
entity: response.value,
|
||||
me: me,
|
||||
networkDate: response.networkDate
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue