Skip to content

Instantly share code, notes, and snippets.

@agiokas
Created February 16, 2020 10:48
Show Gist options
  • Save agiokas/b9dd2e64f91e2a101ce748005b845029 to your computer and use it in GitHub Desktop.
Save agiokas/b9dd2e64f91e2a101ce748005b845029 to your computer and use it in GitHub Desktop.
import CoreData
import Foundation
import os.log
import RxCocoa
import RxSwift
class CDObservable<T>: NSObject, ObservableType, NSFetchedResultsControllerDelegate where T: NSManagedObject {
private var fetchedResultsController: NSFetchedResultsController<NSManagedObject>?
private let context: NSManagedObjectContext
private let fetchRequest: NSFetchRequest<T>
private let results = BehaviorSubject<[T]>(value: [])
private var subscriptions = 0
private let serialQueue = DispatchQueue(label: "com.RxFetcher")
typealias Element = [T]
init(fetchRequest: NSFetchRequest<T>, context: NSManagedObjectContext) {
if fetchRequest.sortDescriptors == nil {
fetchRequest.sortDescriptors = []
}
self.fetchRequest = fetchRequest
self.context = context
super.init()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment