I’ve been utilizing my SwiftUI app for some time and observed that my widget began displaying the placeholder as a substitute of the particular widget in random instances. For the placeholder widget I used template information and it is not meant to be displayed on the house display. The feedback left by the WidgetKit extension template acknowledged that I should not do fetch requests within the placeholders, so I did not.
To create instance gadgets for placeholders or previews I created this operate:
personal func createExampleItem(_ rely: Int = 1) -> [Birth] {
let childContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
childContext.guardian = PersistenceController.shared.container.viewContext
let exampleItem = Beginning(context: childContext)
exampleItem.title = "John Doe"
exampleItem.date = Calendar.present.date(byAdding: .day, worth: 10, to: Date())
do {
attempt childContext.save()
} catch {
print(error)
}
var arr: [Birth] = []
for _ in 1...rely {
arr.append(exampleItem)
}
return arr
}
On this operate I used childContexts in order that my instance information wasn’t saved to the precise viewContext.
Each my getSnapshot
and placeholder
capabilities use the createExampleItem
operate so I truly do not know if the widget displayed on the House Display is the snapshot or placeholder. I can also’t check to see which one it’s because it shows the placeholder in random instances. If this is not a recognized challenge I’ll check and see if it is the snapshot or placeholder being displayed regardless of how lengthy it takes.
I additionally tried refreshing the widgets by way of a button I put within the app however it does not change something.
Timeline Supplier
struct Supplier: IntentTimelineProvider {
var networkManager = NetworkManager()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), gadgets: createExampleItem(), itemCount: 1)
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Entry) -> Void) {
let date = Date()
var entry: SimpleEntry
entry = SimpleEntry(date: date, configuration: configuration, gadgets: createExampleItem(), itemCount: 1)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
networkManager.fetchDataAndFilter { gadgets in
let entry = SimpleEntry(date: Date(), configuration: configuration, gadgets: gadgets, itemCount: gadgets.rely)
let timeline = Timeline(entries: [entry], coverage: .atEnd)
completion(timeline)
}
}
}
Community Supervisor
class NetworkManager {
personal func fetchData() throws -> [Birth] {
let context = PersistenceController.shared.container.viewContext
let request = Beginning.fetchRequest()
let end result = attempt context.fetch(request)
return end result
}
func fetchDataAndFilter(completion: @escaping ([Birth]) -> Void) {
do {
let gadgets = attempt self.fetchData()
let sorted = sortBirthItems(gadgets: gadgets, filter: true)
completion(sorted)
} catch {
print(error)
}
}
}