在我的iOS 14应用中,我正在解析一个RSS feed,并且该Widget应该在数组中显示最新的条目[0]
。这是可行的,但是,当添加任何新条目时,它不会更新。我已经尝试过.atEnd
并设置了一定的分钟数,但它没有改变。为了确认解析器不是问题,我将构建安装在其他设备上,该设备显示了最新条目,而具有第一个构建的原始设备仍仅显示安装时是最新版本的设备。
struct Provider: TimelineProvider {
@State private var rssItems:[RSSItem]?
let feedParser = FeedParser()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), title:"News", description: "News article here", link: "Http://link", pubDate: "The day it posted")
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), title:"News", description: "News Article Here", link: "Http://link", pubDate: "The day it posted")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
var entries: [SimpleEntry] = []
feedParser.parseFeed(url: "https://fritchcoc.wordpress.com/feed") {(rssItems) in
self.rssItems = rssItems
let currentDate = Date()
let string1 = "Latest News: \n\n"
let string2 = rssItems[0].title
var appendString1 = string1+string2
let entry = SimpleEntry(date: currentDate, title:appendString1, description: rssItems[0].description, link: rssItems[0].link, pubDate: rssItems[0].pubDate)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let title: String
let description: String
let link: String
let pubDate: String
}
struct FritchNewsEntryView : View {
var entry: SimpleEntry
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(entry.title)
.font(.system(.title3))
.foregroundColor(.black)
Text(entry.pubDate)
.font(.system(.callout))
.foregroundColor(.black)
.bold()
Text(entry.description)
.font(.system(.caption))
.foregroundColor(.black)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.padding()
.background(LinearGradient(gradient: Gradient(colors: [.orange, .yellow]), startPoint: .top, endPoint: .bottom))
}
}
@main
struct TestWidget: Widget {
let kind: String = "TestWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
FritchNewsEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct TestWidget_Previews: PreviewProvider {
static var previews: some View {
FritchNewsEntryView(entry: SimpleEntry(date: Date(), title: "Some Title", description: "a Description", link: "https://www.apple.com", pubDate: "2020"))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}