2021-04-26 10:57:50 +02:00
|
|
|
//
|
|
|
|
// SettingFetchedResultController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class SettingFetchedResultController: NSObject {
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
let fetchedResultsController: NSFetchedResultsController<Setting>
|
|
|
|
|
|
|
|
// input
|
|
|
|
|
|
|
|
// output
|
2022-09-30 13:28:09 +02:00
|
|
|
public let settings = CurrentValueSubject<[Setting], Never>([])
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public init(managedObjectContext: NSManagedObjectContext, additionalPredicate: NSPredicate?) {
|
2021-04-26 10:57:50 +02:00
|
|
|
self.fetchedResultsController = {
|
|
|
|
let fetchRequest = Setting.sortedFetchRequest
|
|
|
|
fetchRequest.returnsObjectsAsFaults = false
|
|
|
|
if let additionalPredicate = additionalPredicate {
|
|
|
|
fetchRequest.predicate = additionalPredicate
|
|
|
|
}
|
|
|
|
fetchRequest.fetchBatchSize = 20
|
|
|
|
let controller = NSFetchedResultsController(
|
|
|
|
fetchRequest: fetchRequest,
|
|
|
|
managedObjectContext: managedObjectContext,
|
|
|
|
sectionNameKeyPath: nil,
|
|
|
|
cacheName: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
return controller
|
|
|
|
}()
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
fetchedResultsController.delegate = self
|
|
|
|
|
|
|
|
do {
|
|
|
|
try self.fetchedResultsController.performFetch()
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSFetchedResultsControllerDelegate
|
|
|
|
extension SettingFetchedResultController: NSFetchedResultsControllerDelegate {
|
2022-09-30 13:28:09 +02:00
|
|
|
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
let objects = fetchedResultsController.fetchedObjects ?? []
|
|
|
|
self.settings.value = objects
|
|
|
|
}
|
|
|
|
}
|