1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-17 03:09:19 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/FetchedResultsController/SettingFetchedResultController.swift

65 lines
2.0 KiB
Swift

//
// SettingFetchedResultController.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-4-25.
//
import os.log
import UIKit
import Combine
import CoreData
import CoreDataStack
import MastodonSDK
public final class SettingFetchedResultController: NSObject {
var disposeBag = Set<AnyCancellable>()
let fetchedResultsController: NSFetchedResultsController<Setting>
// input
// output
public let settings = CurrentValueSubject<[Setting], Never>([])
public init(managedObjectContext: NSManagedObjectContext, additionalPredicate: NSPredicate?) {
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 {
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
let objects = fetchedResultsController.fetchedObjects ?? []
self.settings.value = objects
}
}