iOS - today-widget does not get latest changes from host app

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.

https://stackoverflow.com/questions/60104060/ios-today-widget-does-not-get-latest-changes-from-host-app


What could be the issue and how to fix it?

Sorry this is a bit off topic but how did you get the generated classes for your core data model to be shared between host and extension app? Do you use "class definion" or manually generate?

Thanks. I used Category/Extesnsion to generate the entity in the host-app. While the entity-file is opened, in Target Membership, I selected tody extesnion too (As you know, the host is selected by default). The host-app and extension share same group identifier

view will appear is not always called, but view did load is. I've noticed. Im having issue with core data not returning any record on first launch. But if I launch the app again from Xcode then everything is fine. I want to smash my head against the walls to say the least...
iOS - today-widget does not get latest changes from host app
 
 
Q