DynamicFetchView.swift 776 B

123456789101112131415161718192021
  1. import CoreData
  2. import SwiftUI
  3. struct DynamicFetchView<T: NSManagedObject, Content: View>: View {
  4. let fetchRequest: FetchRequest<T>
  5. let content: (FetchedResults<T>) -> Content
  6. var body: some View {
  7. self.content(fetchRequest.wrappedValue)
  8. }
  9. init(predicate: NSPredicate?, sortDescriptors: [NSSortDescriptor], @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
  10. fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortDescriptors, predicate: predicate)
  11. self.content = content
  12. }
  13. init(fetchRequest: NSFetchRequest<T>, @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
  14. self.fetchRequest = FetchRequest<T>(fetchRequest: fetchRequest)
  15. self.content = content
  16. }
  17. }