Created
August 6, 2019 13:46
-
-
Save PaulWoodIII/bb2feec6e8e6d129a2854279fcb70325 to your computer and use it in GitHub Desktop.
creating a FetchRequest property based on state passed to a view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NamesByYearView.swift | |
| // NameList | |
| // | |
| // Created by Paul Wood on 7/31/19. | |
| // Copyright © 2019 Paul Wood. All rights reserved. | |
| // | |
| import SwiftUI | |
| import CoreData | |
| struct NamesByYearView: View { | |
| @EnvironmentObject var coreDataStack: CoreDataStack | |
| @Environment(\.managedObjectContext) var managedObjectContext | |
| @ObservedObject var year: YearOfBirth | |
| @EnvironmentObject var importer: NameDatabaseImporter | |
| var years: FetchRequest<CountForNameByYear> | |
| init(year: YearOfBirth) { | |
| self.year = year | |
| self.years = FetchRequest(fetchRequest: CountForNameByYear.fetchRequest(forYear: year)) | |
| } | |
| var body: some View { | |
| VStack { | |
| NamesByYearCollectionView(year: year, context: managedObjectContext) | |
| } | |
| .navigationBarItems(trailing: Text("Total Names: \(years.wrappedValue.count)")) | |
| .navigationBarTitle(Text("Names from \(year.year ?? "")") ) | |
| .onAppear { | |
| self.importer.startParsing(year: self.year) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment