passepartout-apple/Library/Sources/CommonUtils/Business/CoreDataRepository.swift

268 lines
8.5 KiB
Swift
Raw Normal View History

2024-09-23 13:02:26 +00:00
//
// CoreDataRepository.swift
// Passepartout
//
// Created by Davide De Rosa on 8/10/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Combine
import CoreData
import Foundation
public protocol CoreDataUniqueEntity: NSManagedObject, UniqueEntity {
// Core Data entity must have a unique "uuid" field
}
public enum CoreDataResultAction {
case ignore
case discard
case halt
}
2024-09-23 13:02:26 +00:00
public actor CoreDataRepository<CD, T>: NSObject,
Repository,
NSFetchedResultsControllerDelegate where CD: CoreDataUniqueEntity,
T: UniqueEntity {
2024-09-23 13:02:26 +00:00
private let entityName: String
private nonisolated let context: NSManagedObjectContext
2024-09-23 13:02:26 +00:00
private let observingResults: Bool
private let beforeFetch: ((NSFetchRequest<CD>) -> Void)?
private nonisolated let fromMapper: (CD) throws -> T?
2024-09-23 13:02:26 +00:00
private nonisolated let toMapper: (T, NSManagedObjectContext) throws -> CD
2024-09-23 13:02:26 +00:00
private nonisolated let onResultError: ((Error) -> CoreDataResultAction)?
2024-09-23 13:02:26 +00:00
private nonisolated let entitiesSubject: CurrentValueSubject<EntitiesResult<T>, Never>
2024-09-23 13:02:26 +00:00
private var resultsController: NSFetchedResultsController<CD>?
2024-09-23 13:02:26 +00:00
public init(
context: NSManagedObjectContext,
observingResults: Bool,
2024-09-23 13:02:26 +00:00
beforeFetch: ((NSFetchRequest<CD>) -> Void)? = nil,
fromMapper: @escaping (CD) throws -> T?,
toMapper: @escaping (T, NSManagedObjectContext) throws -> CD,
onResultError: ((Error) -> CoreDataResultAction)? = nil
2024-09-23 13:02:26 +00:00
) {
guard let entityName = CD.entity().name else {
fatalError("Unable to find entity name for \(CD.self)")
}
self.entityName = entityName
self.context = context
self.observingResults = observingResults
self.beforeFetch = beforeFetch
2024-09-23 13:02:26 +00:00
self.fromMapper = fromMapper
self.toMapper = toMapper
self.onResultError = onResultError
entitiesSubject = CurrentValueSubject(EntitiesResult())
}
public nonisolated var entitiesPublisher: AnyPublisher<EntitiesResult<T>, Never> {
entitiesSubject
.eraseToAnyPublisher()
}
public func filter(byFormat format: String, arguments: [Any]?) async throws {
try await filter(byPredicate: NSPredicate(format: format, argumentArray: arguments))
}
public func resetFilter() async throws {
try await filter(byPredicate: nil)
}
public func fetchAllEntities() async throws -> [T] {
try await filter(byPredicate: nil)
}
2024-09-23 13:02:26 +00:00
public func saveEntities(_ entities: [T]) async throws {
try await context.perform { [weak self] in
guard let self else {
return
}
do {
let request = newFetchRequest()
let existingIds = entities.compactMap(\.uuid)
request.predicate = NSPredicate(
format: "any uuid in %@",
existingIds
)
let existing = try context.fetch(request)
existing.forEach(context.delete)
2024-09-23 13:02:26 +00:00
for entity in entities {
_ = try self.toMapper(entity, context)
2024-09-23 13:02:26 +00:00
}
try context.save()
} catch {
context.rollback()
throw error
}
}
}
public func removeEntities(withIds ids: [UUID]?) async throws {
2024-09-23 13:02:26 +00:00
try await context.perform { [weak self] in
guard let self else {
return
}
let request = newFetchRequest()
if let ids {
request.predicate = NSPredicate(
format: "any uuid in %@",
ids
)
}
2024-09-23 13:02:26 +00:00
do {
let existing = try context.fetch(request)
existing.forEach(context.delete)
try context.save()
} catch {
context.rollback()
throw error
}
}
}
// MARK: NSFetchedResultsControllerDelegate
// XXX: triggers on entity insert/update/delete and reloads/remaps ALL into entitiesSubject
public nonisolated func controllerDidChangeContent(_ controller: NSFetchedResultsController<any NSFetchRequestResult>) {
guard observingResults else {
return
}
2024-09-23 13:02:26 +00:00
guard let cdController = controller as? NSFetchedResultsController<CD> else {
fatalError("Unable to upcast results to \(CD.self)")
}
Task.detached { [weak self] in
await self?.sendResults(from: cdController)
}
2024-09-23 13:02:26 +00:00
}
}
private extension CoreDataRepository {
enum ResultError: Error {
case mapping(Error)
}
nonisolated func newFetchRequest() -> NSFetchRequest<CD> {
NSFetchRequest(entityName: entityName)
}
@discardableResult
func filter(byPredicate predicate: NSPredicate?) async throws -> [T] {
let request = newFetchRequest()
2024-09-23 13:02:26 +00:00
request.predicate = predicate
beforeFetch?(request)
let newController = try await context.perform {
let newController = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: self.context,
sectionNameKeyPath: nil,
cacheName: nil
)
newController.delegate = self
try newController.performFetch()
return newController
}
resultsController = newController
return await sendResults(from: newController)
2024-09-23 13:02:26 +00:00
}
@discardableResult
func sendResults(from controller: NSFetchedResultsController<CD>) async -> [T] {
await context.perform {
self.unsafeSendResults(from: controller)
}
2024-09-23 13:02:26 +00:00
}
@discardableResult
func unsafeSendResults(from controller: NSFetchedResultsController<CD>) -> [T] {
guard var cdEntities = controller.fetchedObjects else {
return []
}
var entitiesToDelete: [CD] = []
// strip duplicates by sort order (first entry wins)
var knownUUIDs = Set<UUID>()
cdEntities.forEach {
guard let uuid = $0.uuid else {
return
}
guard !knownUUIDs.contains(uuid) else {
NSLog("Strip duplicate \(String(describing: CD.self)) with UUID \(uuid)")
entitiesToDelete.append($0)
return
}
knownUUIDs.insert(uuid)
}
do {
cdEntities.removeAll {
entitiesToDelete.contains($0)
}
let entities = try cdEntities.compactMap {
2024-09-23 13:02:26 +00:00
do {
return try fromMapper($0)
} catch {
switch onResultError?(error) {
case .discard:
entitiesToDelete.append($0)
case .halt:
throw ResultError.mapping(error)
default:
break
}
return nil
}
}
if !entitiesToDelete.isEmpty {
do {
entitiesToDelete.forEach(context.delete)
try context.save()
2024-09-23 13:02:26 +00:00
} catch {
NSLog("Unable to delete Core Data entities: \(error)")
context.rollback()
2024-09-23 13:02:26 +00:00
}
}
let result = EntitiesResult(entities, isFiltering: controller.fetchRequest.predicate != nil)
entitiesSubject.send(result)
return result.entities
} catch {
NSLog("Unable to send Core Data entities: \(error)")
return []
2024-09-23 13:02:26 +00:00
}
}
}