- Add background context support to CoreDataManager - Fix TagEntity threading crashes in LabelsRepository - Prevent WebView height updates during scrolling to reduce flicker - Add App Store download link to README
45 lines
1.3 KiB
Swift
45 lines
1.3 KiB
Swift
import Foundation
|
|
import CoreData
|
|
|
|
class LabelsRepository: PLabelsRepository, @unchecked Sendable {
|
|
private let api: PAPI
|
|
|
|
private let coreDataManager = CoreDataManager.shared
|
|
|
|
init(api: PAPI) {
|
|
self.api = api
|
|
}
|
|
|
|
func getLabels() async throws -> [BookmarkLabel] {
|
|
let dtos = try await api.getBookmarkLabels()
|
|
try? await saveLabels(dtos)
|
|
return dtos.map { $0.toDomain() }
|
|
}
|
|
|
|
func saveLabels(_ dtos: [BookmarkLabelDto]) async throws {
|
|
let backgroundContext = coreDataManager.newBackgroundContext()
|
|
|
|
try await backgroundContext.perform { [weak self] in
|
|
guard let self = self else { return }
|
|
for dto in dtos {
|
|
if !self.tagExists(name: dto.name, in: backgroundContext) {
|
|
dto.toEntity(context: backgroundContext)
|
|
}
|
|
}
|
|
try backgroundContext.save()
|
|
}
|
|
}
|
|
|
|
private func tagExists(name: String, in context: NSManagedObjectContext) -> Bool {
|
|
let fetchRequest: NSFetchRequest<TagEntity> = TagEntity.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "name == %@", name)
|
|
|
|
do {
|
|
let count = try context.count(for: fetchRequest)
|
|
return count > 0
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
}
|