Learn Courses My Dashboard

Refresh a view when app is brought from the background

Hello all,

I’m facing the following conundrum: my app is meant to display a random quote of the day (along with a widget). All quotes and accompanying information are stored in CoreData and then the relevant one for the day is retrived like this:

struct DailyQuoteView: View {

let managedObjectContext = CoreDataStack.shared.managedObjectContext
        sortDescriptors: [NSSortDescriptor(key: "date", ascending: true)],
        predicate: NSPredicate(
            format: "date >= %@ AND date <= %@",
            argumentArray: [
                Calendar.current.startOfDay(for: Date()),
                Calendar.current.date(byAdding: .minute, value: 1439, to: Calendar.current.startOfDay(for: Date()))!
    ) var quote: FetchedResults<QuoteEntity>


I noticed that when I click on the widget on the following day (the widget displays the correct quote) and the app is brought forth from the background, the DailyQuoteView still shows the quote from the previous day until I remove it from the list of open apps and reopen it.

So, here’s my question: is it possible to refresh the view when the app is brought from the background (I stress here: I do not expect this to be done in the background).

Thank you!

I believe so, use the sceneDelegate

Could you please pretend I’m a complete newbie and elaborate a bit? I’ve not used sceneDeletages so far, only SwiftUI.