I have a
NSManagedObject
called Event
shared between the host app and today extension. (In Target Membership, both the main app and the widget are checked). The host app and widget have the same App Group identifier and both share same Data Model
(In Target Membership, both the main app and the widget are checked).When I launch(run) the widget in Xcode, it shows all the events (
Event
) that are already saved in the host app. However, when I add or edit an event, it appears in the host app as so but NOT in today-widget. If I relaunch the widget, all the events are shown including the last event that previously was not.This is the method that fetches events. It is defined in
TodayViewController
of the widget.private func fetchEvents(date: Date) {
let predicates = NSCompoundPredicate(andPredicateWithSubpredicates: [
NSPredicate(format: "date = %@",Date().startOfDay as CVarArg),
NSedicate(format: "startTime >= %@", Date() as CVarArg) ])
if let ev = try? TPEvent.fetchAll(predicates: predicates, in: persistentManager.context) { events = ev }
}
This fetchEvents() is called in
viewWillAppear
and widgetPerformUpdate
. persistentManaged.context
is persistentContainer.viewContext.override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
fetchEvents(date: Date())
self.tableView.reloadData()
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
self.fetchEvents(date: Date() )
self.tableView.reloadData()
completionHandler(NCUpdateResult.newData)
}
I have asked this question on Stackoverflow. It is more descriptive than this here.
What could be the issue and how to fix it?