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!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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
}
}
}
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!