oncall-mobile-ios/ntfy/Persistence/SubscriptionsObservable.swift
2023-11-14 16:00:48 -05:00

60 lines
2.2 KiB
Swift

import CoreData
import SwiftUI
class SubscriptionsObservable: NSObject, ObservableObject {
private let tag = "SubscriptionsObservable"
override init() {
super.init()
// This will force the initialization of notificationsFetchedResultsController
_ = self.notificationsFetchedResultsController
}
private lazy var fetchedResultsController: NSFetchedResultsController<Subscription> = {
let fetchRequest: NSFetchRequest<Subscription> = Subscription.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "topic", ascending: true)]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: Store.shared.context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
do {
Log.d(tag, "Fetching subscriptions")
try controller.performFetch()
} catch {
Log.w(tag, "Failed to fetch subscriptions: \(error)", error)
}
return controller
}()
private lazy var notificationsFetchedResultsController: NSFetchedResultsController<Notification> = {
let fetchRequest: NSFetchRequest<Notification> = Notification.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "time", ascending: true)]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: Store.shared.context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
do {
Log.d(tag, "Fetching notifications")
try controller.performFetch()
} catch {
Log.w(tag, "Failed to fetch notifications: \(error)", error)
}
return controller
}()
var subscriptions: [Subscription] {
fetchedResultsController.fetchedObjects ?? []
}
}
extension SubscriptionsObservable: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
Log.d(tag, "Fetching notifications")
DispatchQueue.main.async {
self.objectWillChange.send()
}
}
}