Mercurial > public > lazybear
view lazybear/Supply views/Watchlist.swift @ 82:0730f031e478
New CoreData is working
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Mon, 25 Jan 2021 12:53:30 +0100 |
parents | |
children | fce6e0364c1c |
line wrap: on
line source
// // Watchlist.swift // LazyBear // // Created by Dennis Concepción Martín on 25/1/21. // import SwiftUI struct Watchlist: View { @Environment(\.managedObjectContext) private var viewContext // Core data @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) // Core data var companies: FetchedResults<WatchlistCompany> // Fetch core data var body: some View { List { ForEach(companies) { company in WatchlistRow(company: company) } // Delete from persistent storage .onDelete { indexSet in delete(indexSet: indexSet) } } } func delete(indexSet: IndexSet) { for index in indexSet { viewContext.delete(companies[index]) } do { try viewContext.save() print("Company deleted") } catch { print(error.localizedDescription) } } } struct Watchlist_Previews: PreviewProvider { static var previews: some View { Watchlist() } }