Post

Replies

Boosts

Views

Activity

Reply to WidgetKit with Data from CoreData
A couple of articles that I have read suggest that I get CoreData records from my DataManager like the following. It's kind of redundant as it accesses DataManager inside the TimelineProvider struct and also inside the Widget struct. That's why I wonder if this is the right approach. import WidgetKit import SwiftUI import CoreData struct Provider: TimelineProvider { func placeholder(in context: Context) -> SimpleEntry { ... } func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { ... } func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { let manager = CloudDataManager() var records: [Little] = [] let request = NSFetchRequest<Little>(entityName: "Little") do { records = try manager.context.fetch(request) records.sort { lhs, rhs in lhs.trashDate! < rhs.trashDate! } } catch { print("Fetch error for CloudDataManager: \(error.localizedDescription)") } var items: [Item] = [] for record in records { let item = Item(trashDate: record.trashDate ?? Date.now, imageSelection: Int(record.imageSelection)) items.append(item) } let entry = Timeline(entries: [SimpleEntry(date: Date(), items: items)], policy: .atEnd) completion(entry) } } struct SimpleEntry: TimelineEntry { let date: Date let items: [Item] } struct LittleGuyEntryView : View { var entry: Provider.Entry var body: some View { ZStack { ContainerRelativeShape() .fill(.widgetBackground) VStack { if entry.items.count > 0 { ForEach(entry.items, id: \.id) { item in ... } } } .padding() } } } struct LittleGuy: Widget { let manager = DataManager() let kind: String = "App name" var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in LittleTrashEntryView(entry: entry) .environment(\.managedObjectContext, manager.context) .containerBackground(.fill.tertiary, for: .widget) } .contentMarginsDisabled() .supportedFamilies([.systemSmall]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to WidgetKit with Data from CoreData
Thanks for the tip, darkpaw. But I'm afraid you misunderstand my question. It's underlined. Do I have to let the widget pull data directly from CoreData data manager (DataManager)? In other words, is there any way I could use my existing view model (ViewModel) to deliver data to the widget? You seem to be writing your fetchRecords() method to get your data inside the getTimeline() method, i.e.: That should be put into your DataManager() class. Okay. Thanks. I have done all dirty work through my view model (ViewModel) including the data sorting. That's why I want to know if I need to do it with the data manager, again, just for the widget. That's what I mean by 'redundant' or superfluous.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to NWPathMonitor Failing
Hello. Thank you for your feedback, The Eskimo. It's well noted. I'll take a deeper look. I check the network connection with it (NWPathMonitor) so that the users are able to access App Store to make in-app purchases. If they are not connected, I just show some text with a progress guy, never showing the purchase list.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to NWPathMonitor Failing
I appreciate your kind recommendation. I realize that NWPathMonitor can return unexpected results. That has initiated this question in the first place. In my app, I have employed a measure as to what to do when it ges an unexpected connection result. Yet, I'll see if I need additional measures.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25