From 9c662f4d78d7051499c1c4266b219df7ffa57ef6 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Thu, 23 Nov 2023 14:30:46 +0100 Subject: [PATCH] Remove FetchedResultsController for SearchHistory (IOS-196) --- ...SearchHistoryFetchedResultController.swift | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 MastodonSDK/Sources/MastodonCore/FetchedResultsController/SearchHistoryFetchedResultController.swift diff --git a/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SearchHistoryFetchedResultController.swift b/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SearchHistoryFetchedResultController.swift deleted file mode 100644 index f8376a9c0..000000000 --- a/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SearchHistoryFetchedResultController.swift +++ /dev/null @@ -1,77 +0,0 @@ -// -// SearchHistoryFetchedResultController.swift -// Mastodon -// -// Created by MainasuK Cirno on 2021-7-15. -// - -import UIKit -import Combine -import CoreData -import CoreDataStack -import MastodonSDK - -public final class SearchHistoryFetchedResultController: NSObject { - - var disposeBag = Set() - - public let fetchedResultsController: NSFetchedResultsController - public let domain = CurrentValueSubject(nil) - public let userID = CurrentValueSubject(nil) - - // output - let _objectIDs = CurrentValueSubject<[NSManagedObjectID], Never>([]) - @Published public private(set) var records: [ManagedObjectRecord] = [] - - public init(managedObjectContext: NSManagedObjectContext) { - self.fetchedResultsController = { - let fetchRequest = SearchHistory.sortedFetchRequest - fetchRequest.returnsObjectsAsFaults = false - fetchRequest.fetchBatchSize = 20 - let controller = NSFetchedResultsController( - fetchRequest: fetchRequest, - managedObjectContext: managedObjectContext, - sectionNameKeyPath: nil, - cacheName: nil - ) - - return controller - }() - super.init() - - // debounce output to prevent UI update issues - _objectIDs - .throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true) - .map { objectIDs in objectIDs.map { ManagedObjectRecord(objectID: $0) } } - .assign(to: &$records) - - fetchedResultsController.delegate = self - - Publishers.CombineLatest( - self.domain, - self.userID - ) - .receive(on: DispatchQueue.main) - .sink { [weak self] domain, userID in - guard let self = self else { return } - let predicates = [SearchHistory.predicate(domain: domain ?? "", userID: userID ?? "")] - self.fetchedResultsController.fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) - do { - try self.fetchedResultsController.performFetch() - } catch { - assertionFailure(error.localizedDescription) - } - } - .store(in: &disposeBag) - } - -} - -// MARK: - NSFetchedResultsControllerDelegate -extension SearchHistoryFetchedResultController: NSFetchedResultsControllerDelegate { - public func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) { - - let objects = fetchedResultsController.fetchedObjects ?? [] - self._objectIDs.value = objects.map { $0.objectID } - } -}