1

so I've a swift-ui app where I show information for the current day - so at midnight I want it to switch to a new day - trouble is I want this to be as "light" as possible - eg I don't want anything consuming any resources as all I want to do is refresh the view, if it's displayed.

How do I do this without consuming background resources when the view isn't shown?

Darren Oakey
  • 2,894
  • 3
  • 29
  • 55
  • Does this answer your question? [Checking when a date has passed - Swift](https://stackoverflow.com/questions/35525921/checking-when-a-date-has-passed-swift) – rbaldwin Jul 04 '21 at 07:08

1 Answers1

3

you could try something like this: (note I did not wait to see if this works)

import SwiftUI
import Combine
#if os(iOS)
import UIKit
#endif


@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        Text("day change")
            .onReceive(NotificationCenter.default.publisher(
                for: UIApplication.significantTimeChangeNotification)) { _ in
                
                print("----> day time has changed <----\n")
                
                print("A notification that posts when there is a significant change in time, \n for example, change to a new day (midnight), \n carrier time update, and change to or from daylight savings time.")
            }
    }
}