Skip to content

Instantly share code, notes, and snippets.

@acwright
Created November 23, 2019 04:05
Show Gist options
  • Save acwright/6dec0eb34a8d11c50d064dd43edb840e to your computer and use it in GitHub Desktop.
Save acwright/6dec0eb34a8d11c50d064dd43edb840e to your computer and use it in GitHub Desktop.
import SwiftUI
import CoreData
struct FetchedObjects<T, Content>: View where T : NSManagedObject, Content : View {
// MARK: - Properties
let content: ([T]) -> Content
var request: FetchRequest<T>
var results: FetchedResults<T>{ request.wrappedValue }
// MARK: - Lifecycle
init(
predicate: NSPredicate = NSPredicate(value: true),
sortDescriptors: [NSSortDescriptor] = [],
@ViewBuilder content: @escaping ([T]) -> Content
) {
self.content = content
self.request = FetchRequest(
entity: T.entity(),
sortDescriptors: sortDescriptors,
predicate: predicate
)
}
// MARK: - Body
var body: some View {
self.content(results.map { $0 })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment