So I'm making this app in which the user can store data in CoreData and see it in a table view.
This works like it should, but now I want the data to been seen in a table view, which is located in a today widget.
I already tried to do this the "regular way" by trying to fetch, but the widget isn't able to read it via the AppDelegate file.
The error that I get says the following : " 'shared' is unavailable: Use view controller based solutions where appropriate instead."
I also get the error : "Use of undeclared type 'AppDelegate'"
Is anyone capable in helping me?
Thanks,
EDIT :
This is the code I use to fetch the data in the ViewController :
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSManagedObject>(entityName: "ToDoList")
do {
toDoItems = try managedContext.fetch(request)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
toDoTableView.tableFooterView = UIView()
self.toDoTableView.reloadData()
toDoTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = UITableViewCell()
cell.contentView.backgroundColor = UIColor.clear
cell.backgroundColor = UIColor.clear
toDoTableView.backgroundColor = UIColor.clear
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return toDoItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let activities = toDoItems[indexPath.row]
let cell = toDoTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text =
activities.value(forKeyPath: "todoitems") as? String
return cell
}
}
Please note that this is not my entire code, but some fragments which all together create the fetch function