Post

Replies

Boosts

Views

Activity

SwiftUI + Core Data: unable to delete entity
I have created this TimerListView with (+) button on the toolbar. I'm able to create a new Timer entity using TimerEditView. But if in the TimerEditView I try to delete the entity clicking on "Dismiss" button, an error will be raised. import SwiftUI import CoreData struct TimerListView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Timer.id, ascending: true)], animation: .default) private var timers: FetchedResults<Timer> @State private var isPresentingSheet = false @State private var showFavoritesOnly = false @State private var sheetView: SheetView = .setting @State var newTimerToAdd: Timer = Timer() var body: some View { NavigationView{ List { ForEach(timers) { timer in NavigationLink(destination: TimerDetailView(timer: timer)) { TimerCardView(timer: timer) } } } .navigationTitle("Timer") .toolbar { ToolbarItem(placement: .confirmationAction) { Button(action: { newTimerToAdd = Timer(context: viewContext) newTimerToAdd.id = UUID() newTimerToAdd.name = "Timer name" newTimerToAdd.secondsOn = 0 newTimerToAdd.secondsOff = 0 newTimerToAdd.rounds = 1 newTimerToAdd.cycles = 1 newTimerToAdd.secondsOffBetweenCycles = 0 newTimerToAdd.favourite = false sheetView = .newTimer isPresentingSheet = true }) { Label("New timer", systemImage: "plus") } } } .sheet(isPresented: $isPresentingSheet) { if (sheetView == .newTimer) { NavigationView { TimerEditView(timer: newTimerToAdd) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Dismiss") { viewContext.delete(newTimerToAdd) do { try viewContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. viewContext.rollback() //print("Failed to save context \(nsError.localizedDescription)") let nsError = error as NSError fatalError("CAZZO Unresolved error \(nsError), \(nsError.userInfo)") } isPresentingSheet = false } } ToolbarItem(placement: .confirmationAction) { Button("Add") { do { try viewContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } //timers.append(newTimerToAdd) isPresentingSheet = false //newTimer = Timer() } } } // .toolbar } // NavigationView } else { NavigationView { SettingsView() .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Salva") { isPresentingSheet = false } } } } } } //.sheet } } }
1
0
1k
Sep ’22
SwiftUI + Core Data: list doesn't refresh changes (no relationship)
Recently I have integrated Core Data to my SwiftUI App. Just to simplify, I have two views: TimerListView: fetch data from Core Data and show the list TimerDetailView: show Timer details and update data Where struct TimerListView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Timer.id, ascending: true)], animation: .default) private var timers: FetchedResults<Timer> var body: some View { NavigationView{ List { ForEach(timers) { timer in NavigationLink(destination: TimerDetailView(timer: timer)) { TimerCardView(timer: timer) } } } } [...] } } and struct TimerDetailView: View { @Environment(\.managedObjectContext) private var viewContext @ObservedObject var timer: Timer } Outcomes: Data is correctly updated in Core Data (if I close and reopen the App data is correctly updated) When from TimerDetailView I turn back to TimerListView (after an update) the list doesn't show the updated data Why the list doesn't show updated data automatically? Thanks!
1
0
716
Sep ’22
SwiftUI background mode works in simulator but not on a real device?
I'm playing an Audio in my app. I can play the audio in the background mode in the simulator but when I test the app in a real device, the audio Stops as soon as the App goes in the background. Xcode 14 iOS 16 Device: iPhone 11 I have this in the background mode: This is the code into .onAppear() view: let path = Bundle.main.path(forResource: "timer.mp3", ofType:nil)! let url = URL(fileURLWithPath: path) timerEffect = try AVAudioPlayer(contentsOf: url) //@State var timerEffect: AVAudioPlayer? timerEffect?.numberOfLoops = -1 timerEffect?.play() Anyone can help me? Thanks!
3
0
3.1k
Aug ’22