I’ve a easy app that makes use of CoreData outlined as follows:
let fetchRequest: NSFetchRequest<ListObject> = ListObject.fetchRequest()
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: self.viewContext,
sectionNameKeyPath: nil,
cacheName: nil)
fetchedResultsController.performFetch()
var objects = fetchedResultsController.fetchedObjects
let container = NSPersistentContainer(title: "AppModel")
let storeURL = URL.storeURL(for: "group.firm.group", databaseName: "AppModel")
let storeDescription = NSPersistentStoreDescription(url: storeURL)
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores { storeDescription, error in
self.viewContext = container.viewContext
self.viewContext.automaticallyMergesChangesFromParent = true
self.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
self.fetchedResultsController.performFetch()
}
// Save
viewContext.carry out {
if self.viewContext.hasChanges {
strive self.viewContext.save()
WidgetCenter.shared.reloadAllTimelines()
}
}
// Widget
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let simpleEntry = createEntry(index:0)
let entries = [simpleEntry]
let nextUpdateDate = Calendar.present.date(byAdding: .hour, worth: 1, to: Date())!
let timeline = Timeline(entries: entries, coverage: .after(nextUpdateDate))
}
Nonetheless after I name ‘save()’ within the app, then dismiss the app, the corresponding CoreData replace will not be out there within the widget (fairly the previous knowledge nonetheless persists, virtually as if its being cached).
I’ve tried DarwinNotifications, making certain ‘Background fetch/processing’ is enabled, referred to as reloadAllTimelines, tried to make sure all saves/reads are on the identical thread (viewContext.carry out) – nonetheless nothing appears to work.
Is there a method to power a widget to replace with the newest knowledge from CoreData?