Skip to content

Instantly share code, notes, and snippets.

@daniloc
Last active May 7, 2021 20:54
Show Gist options
  • Save daniloc/65f79c3fe1d77c3e82d29b0d15af59a9 to your computer and use it in GitHub Desktop.
Save daniloc/65f79c3fe1d77c3e82d29b0d15af59a9 to your computer and use it in GitHub Desktop.

Revisions

  1. daniloc revised this gist Aug 28, 2020. No changes.
  2. daniloc revised this gist Aug 28, 2020. No changes.
  3. daniloc revised this gist Aug 28, 2020. No changes.
  4. daniloc revised this gist Aug 28, 2020. No changes.
  5. daniloc revised this gist Aug 28, 2020. No changes.
  6. daniloc created this gist Aug 28, 2020.
    24 changes: 24 additions & 0 deletions EntityIterator.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    //adapted via: https://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui

    import SwiftUI
    import CoreData

    struct EntityIterator<Entity: NSManagedObject, Content: View>: View {

    var fetchRequest: FetchRequest<Entity>
    var results: FetchedResults<Entity> { fetchRequest.wrappedValue }

    let content: (Entity) -> Content

    var body: some View {
    ForEach(fetchRequest.wrappedValue, id: \.self) { result in
    self.content(result)
    }
    }

    init(predicate: NSPredicate, sortDescriptors: [NSSortDescriptor], @ViewBuilder content: @escaping (Entity) -> Content) {
    fetchRequest = FetchRequest<Entity>(entity: Entity.entity(), sortDescriptors: sortDescriptors, predicate: predicate)

    self.content = content
    }
    }