reloadAllTimelines 不适用于 UserDefaults
Posted
技术标签:
【中文标题】reloadAllTimelines 不适用于 UserDefaults【英文标题】:reloadAllTimelines not working with UserDefaults 【发布时间】:2020-10-14 19:27:52 【问题描述】:我的小部件从UserDefaults/ Appgroups
加载一些数据,并根据这些数据显示一些文本和图片。
这适用于第一次启动。
如果我更改 UserDefaults 并使用 WidgetCenter.shared.reloadAllTimelines()
它只会更改图片而不是文本。
我能做什么?是否有其他选项可以在主应用程序和小部件之间共享数据?似乎是一个错误。
这是我的小部件条目:
struct Provider: IntentTimelineProvider
let networkManager = NetworkManager()
func placeholder(in context: Context) -> SimpleEntry
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubnamehome: "test", clubnameaway: "test2")
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ())
networkManager.fetchData (post) in
let entry = SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name)
completion(entry)
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ())
networkManager.fetchData (post) in
let entries = [ SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name) ]
let reloadTime = Calendar.current.date(byAdding: .minute, value: 5, to: Date())!
let timeline = Timeline(entries: entries, policy: .after(reloadTime))
completion(timeline)
这里我设置了 UserDefault 的值:
@IBAction func losGehts(_ sender: UIButton)
switch team
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
if #available(ios 14.0, *)
WidgetCenter.shared.reloadAllTimelines()
else
// Fallback on earlier versions
case "augsburg":
这是我的观点:
struct MyTeamWidgetEntryView : View
@Environment(\.colorScheme) var colorScheme
var imageIcon = "stuttgart"
var entry: Provider.Entry
let logo = UserDefaults(suiteName: "...")!.string(forKey: "logo")
let mannschaftsName = UserDefaults(suiteName: "...")!.string(forKey: "teamname")
let teamID = "289"
var body: some View
HStack
Spacer()
VStack (alignment: .leading, spacing: 0)
Spacer().frame(height: 10)
// image von hier
HStack
Spacer()
switch logo
case "arminia":
Image("bundesliga1/arminia").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
case "augsburg":
Image("bundesliga1/augsburg").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
default:
Image("bundesliga1/...").resizable().aspectRatio(contentMode: .fit).frame(width: 90, height: 90, alignment: .center)
Spacer()
// image bis hier
Spacer().frame(height: 5)
Spacer()
VStack (alignment: .leading, spacing: 6)
VStack
HStack
Text(entry.clubnamehome).font(.system(size: 10)).bold()
Text(":").font(.system(size: 10)).bold()
Text(entry.clubnameaway).font(.system(size: 10)).bold()
Spacer()
//überlegen ob spacer rein soll oder nicht
【问题讨论】:
【参考方案1】:您似乎正确读取了 App Group UserDefaults 中的 logo
和 mannschaftsName
值:
struct MyTeamWidgetEntryView : View
let logo = UserDefaults(suiteName: "...")!.string(forKey: "logo")
let mannschaftsName = UserDefaults(suiteName: "...")!.string(forKey: "teamname")
现在您需要确保在主应用程序中正确设置这些值。在您发布的代码中,您仅为 tabellenId
键设置了一个值:
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
您还需要更新logo
和mannschaftsName
:
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
UserDefaults(suiteName: "...")!.set("???", forKey: "logo")
UserDefaults(suiteName: "...")!.set("???", forKey: "mannschaftsName")
这可能会被提取,所以你不要重复自己:
switch team
...
UserDefaults(suiteName: "...")!.set(team, forKey: "mannschaftsName")
【讨论】:
以上是关于reloadAllTimelines 不适用于 UserDefaults的主要内容,如果未能解决你的问题,请参考以下文章