0

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

Jenoah Kers
  • 157
  • 2
  • 2
  • 7
  • Can you show some code? – ronatory Jan 22 '17 at 12:14
  • @ronatory Sure, what code do you want? The part where I insert the data, fetch the data, the AppDelegate or the TodayWidget? – Jenoah Kers Jan 22 '17 at 12:19
  • Thanks for the edit. So I think these answers should bring you in the right direction to solve your problem http://stackoverflow.com/a/29722425/5327882 and http://stackoverflow.com/a/25408472/5327882 – ronatory Jan 22 '17 at 15:09
  • @ronatory Thanks for linking me through. I only cannot really figure out where I should place the code though, because if I just can reference to the AppDelegate from the TodayWidget extension, then I'm fine, but I don't know how, even though you linked me through. – Jenoah Kers Jan 22 '17 at 15:31
  • Welcome, so as mentioned in the first answer a today extension isn't an application. Because of that you can't use AppDelegate, see also here http://stackoverflow.com/a/28184447/5327882. The steps for you should be clear with a bit of research. You should enable App Groups for your main app and the extension to have a container for your core data store, which you can then access with both your app and your today extension to fetch and write data. There are enough resources available when you google a little bit – ronatory Jan 22 '17 at 15:53

0 Answers0