Hi,
my widget does a json request at the beginning.
But it doesn't stop. It makes 10-20 requests per second. How can I stop that?
This is the timeline of my code:
struct Provider: IntentTimelineProvider {
This is my JSON Request:
my widget does a json request at the beginning.
But it doesn't stop. It makes 10-20 requests per second. How can I stop that?
This is the timeline of my code:
struct Provider: IntentTimelineProvider {
Code Block let networkManager = NetworkManager() func placeholder(in context: Context) -> SimpleEntry { SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubname: networkManager.clubName) } func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { let entry = SimpleEntry(date: Date(), configuration: configuration, clubname: networkManager.clubName) completion(entry) } func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { var entries: [SimpleEntry] = [] let currentDate = Date() for hourOffset in 0 ..< 5 { let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entry = SimpleEntry(date: entryDate, configuration: configuration, clubname: networkManager.clubName) entries.append(entry) } let nextUpdate = Calendar.current.date(byAdding: .hour, value: 1, to: Date()) let timeline = Timeline(entries: entries, policy: .after(nextUpdate)) completion(timeline) } }
This is my JSON Request:
Code Block class NetworkManager: ObservableObject { @Published var posts = [Post]() @Published var clubName = "..." @Published var teamId = "30" @Published var gastMannschaft = "..." let testStr = UserDefaults(suiteName: "....")!.string(forKey: "test") func fetchData() { let teamId = testStr ?? "47" if let url = URL(string: "...=" + teamId) {. let session = URLSession(configuration: .default) let task = session.dataTask(with: url) { (gettingInfo, response, error) in if error == nil { let decoder = JSONDecoder() if let safeData = gettingInfo { do { let results = try decoder.decode(Results.self, from: safeData) DispatchQueue.main.async { self.clubName = results.data[0].away_name if #available(iOS 14.0, *) { WidgetCenter.shared.reloadAllTimelines() } else { // Fallback on earlier versions } } } catch { print(error) } } } } task.resume() } } }