Why is my widget drawn twice?

Hi there,

i have just made a widget, whoohoo! As you can see in the code below, it draws an icon and a random number.

This works fine, however, i have noticed when running, that it is drawn twice every time. That is, first a number is shown, then a second later, it changes again. This seems unneccessary, and i don't understand why it happens.

Hoping that someone can show me the light. Code:


import SwiftUI

import Intents



struct Provider: IntentTimelineProvider {

    func placeholder(in context: Context) -> WidgetEntry {

        print(">>>> MyWidget;getTimeline")

        return WidgetEntry(status: 0, date: Date(), configuration: ConfigurationIntent())

    }



    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (WidgetEntry) -> ()) {

        print(">>>> MyWidget;getSnapshot")

        let entry = WidgetEntry(status: 0, date: Date(), configuration: configuration)

        completion(entry)

    }



    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {

        print(">>>> MyWidget;getTimeline")

        let entry = WidgetEntry(status: 0, date: Date(), configuration: configuration)

        let timeline = Timeline(entries: [entry], policy: .never)

        completion(timeline)

    }

}



struct WidgetEntry: TimelineEntry {

    let status: Int

    let date: Date

    let configuration: ConfigurationIntent

}



struct WidgetView : View {

    var entry: Provider.Entry

    

    init(entry: Provider.Entry){

        print(">>>> WidgetView;init()")

        self.entry = entry;

    }



    var body: some View {

        //Text(entry.date, style: .time)

        VStack{

            Image("check")

            Text("# \(Int.random(in: 1..<6))")

        }

        

    }

}



@main

struct MyWidget: Widget {

    let kind: String = "MyWidget"

    



    var body: some WidgetConfiguration {

        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in

            WidgetView(entry: entry)

        }

        .configurationDisplayName("My Widget")

        .description("Leeloo Dallas Multipass.")

        .supportedFamilies([.accessoryInline, .accessoryCircular, .systemSmall])

    }

}



struct MyWidget_Previews: PreviewProvider {

    static var previews: some View {

        WidgetView(entry: WidgetEntry(status: 0, date: Date(), configuration: ConfigurationIntent()))

            .previewContext(WidgetPreviewContext(family: .accessoryCircular))

    }

}

Home screen widgets are drawn twice per timeline entry: once for light mode and once for dark mode. That allows it to switch between the modes instantly without having to rerun the widget extension.

Lock screen widgets are drawn many more times that that: they render once for every combination of: dark/light mode, vibrancy, privacy, etc.

Hey, thanks for responding.

Ok, that was clarifying, however I must as a follow-up question. If it is created twice, once for light, and once for dark, why am i seeing it being redrawn on-screen? My widget should be displaying the "dark mode version", so it should not be visibly reloaded (i.e. the number changes twice) just because light mode is created?

EDIT: Also, as a side note i just came to think of - If my widget does not support dark/light mode, i.e. it always returns the same, as in this case, wouldn't make sense that i could config that? If my widget takes a lot of resources to create itself (like heavy network calls, calculations etc), it seems like a waste to create the same thing twice, or more, times.

Why is my widget drawn twice?
 
 
Q