Widget Not Refreshing.

I made a widget that refresh every 15 minutes but it doesn't refresh it just shows the place holder text.

I think I have an issue in my Timeline Provider.

Please let me know to Refresh it!

import WidgetKit

import SwiftUI



struct Model : TimelineEntry{

    var date : Date

    var panchangData : [PanchangData]

}



struct Provider : TimelineProvider {

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

        let loadingData = Model(date: Date(), panchangData: Array(repeating: PanchangData(tithi: "Refresh", nakshatra: "Refresh", requestsremaining: 5000, requeststotal: 5000), count: 1))

        

        return loadingData

        

    }

    

    

    

    func getSnapshot(in context: Context, completion: @escaping (Model) -> Void) {

        let loadingData = Model(date: Date(), panchangData: Array(repeating: PanchangData(tithi: "Refresh", nakshatra: "Refresh", requestsremaining: 5000, requeststotal: 5000), count: 1))

        

        completion(loadingData)

    }

    

    func getTimeline(in context: Context, completion: @escaping (Timeline<Model>) -> Void) {

        

        getData{ (modelData) in

            

            let date = Date()

            let data = Model(date: date, panchangData: modelData)

            

            let nextUpdate = Calendar.current.date(byAdding: .minute, value: 15, to: date)

            

            let timeline = Timeline(entries: [data], policy: .after(nextUpdate!))

            

            completion(timeline)

        }

    }

}



func getData(completion: @escaping ([PanchangData])->()){

    

    @AppStorage("userid") var userId: String = "NOTSHOWN"

    @AppStorage("authcode") var authCode: String = "NOTSHOWN"

    

    let today = Date()

    let formatter1 = DateFormatter()

    let formatter2 = DateFormatter()

    let formatter3 = DateFormatter()

    

    

    formatter1.dateFormat = "dd/MM/yyyy"

    formatter2.dateFormat = "HH:mm:ss"

    formatter3.dateFormat = "Z"

    let url = "https://api.panchang.click/v0.4/panchangapi?date=\((formatter1.string(from: today)))&time=\((formatter2.string(from: today)))&tz=\((formatter3.string(from: today)))&userid=\(userId)&authcode=\(authCode)"

    

    let session = URLSession(configuration: .default)

    

    session.dataTask(with: URL(string: url)!){ (data, _,err) in

        

        if err != nil{

            print(err!.localizedDescription)

            

            return

        }

        

        do{

            let jsonData = try JSONDecoder().decode([PanchangData].self, from: data!)

            

            completion(jsonData)

        }catch{

            print(error.localizedDescription)

        }

        

    }

    

    

    struct WidgetView : View{

        

        var data : Model

        

        var body: some View{

            

            ForEach(data.panchangData,id: \.self){ value in

                

                Text(value.nakshatra)

            }

            

            

        }

    }

    @main

    struct MainWidget : Widget{

        

        var body: some WidgetConfiguration{

            StaticConfiguration(kind: "TestWidget1", provider: Provider()){ data in

                WidgetView(data: data)

            }

            .description(Text("test"))

            .configurationDisplayName(Text("test23"))

            .supportedFamilies([.systemSmall])

        }

        

    }

    

    struct WidgetView_Previews: PreviewProvider {

        static var previews: some View {

            WidgetView(data: Model(date: Date(), panchangData: Array(repeating: PanchangData(tithi: "Refresh", nakshatra: "Refresh", requestsremaining: 5000, requeststotal: 5000), count: 1)))

                .previewContext(WidgetPreviewContext(family: .systemSmall))

        }

    }

}
struct PanchangData: Decodable, Hashable{

    var tithi: String

    var nakshatra: String

    var requestsremaining: Int

    var requeststotal: Int


}

I don't know if this is the full code or reduced for display but there are a couple things I would suggest looking at:

  • Your View is not valid. You can't put a bunch of Text() views directly in the body. Try putting a VStack around the ForEach loop.
  • Inside of the dataTask block, make sure that the data object is not nil. You are force unwrapping the optional, but without ever making sure that it isn't nil.
Widget Not Refreshing.
 
 
Q