Виджет iOS 14 не обновляется

Я пытаюсь создать виджет iOS 14, который обновляется каждые две минуты. Это код виджета:

import WidgetKit
import SwiftUI
import Intents

struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: ConfigurationIntent
}

struct Provider: IntentTimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), configuration: ConfigurationIntent())
    }

    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), configuration: configuration)
        completion(entry)
    }

    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        let entry1 = SimpleEntry(date: Date(), configuration: configuration)

        let timeline = Timeline(entries: [entry1], policy: .after(Date().addingTimeInterval(2*60.0)))
        print("timeline: \(timeline)")
        completion(timeline)
    }
}

struct TeamWidgetEntryView : View {
    @Environment(\.widgetFamily) var family
    var entry: Provider.Entry

    @ViewBuilder
    var body: some View {

        switch family {
            case .systemSmall:
                SmallView(date: Date())
            case .systemMedium:
                MediumView()
            case .systemLarge:
                LargeView()
            default:
                Text("Some other WidgetFamily in the future.")
        }
    }
}

@main
struct TeamWidget: Widget {
    let kind: String = "TeamWidget"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            TeamWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemSmall,.systemMedium,.systemLarge])
    }
}

Это класс SmallView:

struct SmallView: View {
    var date: Date

    static let taskDateFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.timeStyle = .medium
        return formatter
    }()
    
    var body: some View {
        VStack {
            Text("Small View")
            Text("\(Date(), formatter: Self.taskDateFormat)")
        }
    }
}

Я хочу, чтобы виджет обновлялся каждые 2 минуты, но этого не происходит, знаете, в чем проблема?


person YosiFZ    schedule 02.12.2020    source источник
comment
Отвечает ли это на ваш вопрос? Установка интервала обновления TimelineProvider для виджета   -  person pawello2222    schedule 02.12.2020
comment
Вы нашли решение?   -  person atalayasa    schedule 15.12.2020


Ответы (2)


Я также столкнулся с той же проблемой и пробовал каждое время обновления от 10 секунд до 10 минут, и кажется, что он начинает обновлять контент только с 5 минут.

person Krokonoxx    schedule 11.12.2020

это то, что я использую для обновления своего виджета каждую минуту

func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
            var entries = [SimpleEntry]()
            let currentDate = Date()
            let midnight = Calendar.current.startOfDay(for: currentDate)
            let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
            for offset in 0 ..< 60 * 24 {
                let entryDate = Calendar.current.date(byAdding: .minute, value: offset, to: midnight)!
                entries.append(SimpleEntry(date: entryDate))
            }
            
            let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
            completion(timeline)
        }
person Bernard    schedule 19.12.2020