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
Reply to Opening FileDocument with URL → should only be called in the main thread
Try using the .task view modifier and removing the Task {} from the function instead. Thanks. But that's not really the main issue here.
Topic: UI Frameworks SubTopic: General Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to onReceive(_:perform:) on Frontmost Window Only?
In your case, can't you add a user info, with the ID of the window for instance ? And test in each window if the notification if for itself. Thanks for the tip. I don't know if that works. You wouldn't still be able to tell which tab is the one currently being selected, I suppose.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’25
Reply to NWPathMonitor Failing
Safari does return the web content if I use an iPhone simulator. Yet, NWPathMonitor fails.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to NWPathMonitor Failing
I didn't notice, but I guess I have to see the status change with onChange(of:initial:_:). Silly me...
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’25
Reply to CoreData Data Sharing with AppGroup
Oops... let container: NSPersistentContainer should be let container: NSPersistentCloudKitContainer I've made a silly mistake two days in a row. I wish I could find a cave to hide myself.
Replies
Boosts
Views
Activity
May ’25
Reply to Can You Debug a Widget on Actual Device?
Houston, I think I have a problem. If I run a sample iOS app with a Widget, I can search and find it. This isn't good.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Can You Debug a Widget on Actual Device?
Houston, now, the Widget appears on my iPhone and iPad. I don't get it. I did change the deployment target to iOS 17.6 before it appeared on my devices. The OS version on my iPad is 18.5. I don't know why it didn't appear on my iPad earlier.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to NWPathMonitor Failing
Okay. I've found some time going over your comments, The Eskimo. You are right. The initial lines of code I have shown are unnecessarily redundant. Thank you.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’25
Reply to App Stuck waiting for review for 24+ days
There really isn't anything that you can do other than waiting. I had my app submission stuck for days because the app used a new API introduced by Apple that had never gone through the review team if I remember correctly. That was several years ago.
Replies
Boosts
Views
Activity
May ’25
Reply to Opening FileDocument with URL → should only be called in the main thread
I've upgraded my system to macOS 15.5. The bug has been system.
Topic: UI Frameworks SubTopic: General Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Constraining Beacon with CLBeaconIdentityCondition
I find it odd that the online document about CLBeaconIdentityConstraint in Swift leads to the online document for CLBeaconIdentityCondition in Objective-C.
Replies
Boosts
Views
Activity
Jun ’25