Populate TableView from CoreData with NSFetchRequest - swift

I need to populate my table view with the data from my core data.
I can display my rows but, I can't populate my tableview!
I use Swift 3 and Xcode 8.
This is my code, in this controller I have to display the list.
class iTableViewController: UITableViewController, NSFetchedResultsControllerDelegate{
#IBOutlet var iTableView: UITableView!
override func viewDidLoad(){
super.viewDidLoad()
iTableView.dataSource = self
iTableView.delegate = self
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managerContext = appDelegate.persistentContainer.viewContext
let interventsFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Intervento")
do{
//results
let fetchedIntervents = try managerContext.fetch(interventsFetch) as! [Intervento]
for interv in fetchedIntervents{
print(interv.stampRow())
}
NSLog(String(fetchedIntervents.count))
}catch{
fatalError("Failed to fetch employees: \(error)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//number of sections
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//numer of rows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
//print the example cell
cell.textLabel?.text = "title"
cell.detailTextLabel?.text = "subtitle"
return cell
}
//swipe and delete
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if(editingStyle == UITableViewCellEditingStyle.delete){
//TODO
}
}
// click cell
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
performSegue(withIdentifier: "segueDetail", sender: self)
}
override func viewDidAppear(_ animated: Bool){
//iTableView.reloadData()
}
override func viewWillDisappear(_ animated: Bool) {
//iTableView.reloadData()
//fetchData()
}
}
this is my class
extension Intervento {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Intervento> {
return NSFetchRequest<Intervento>(entityName: "Intervento");
}
#NSManaged public var cliente: Int32
#NSManaged public var dataFine: String?
#NSManaged public var dataInizio: String?
#NSManaged public var descrizione: String?
#NSManaged public var foto: String?
#NSManaged public var id: Int32
#NSManaged public var stato: String?
#NSManaged public var tecnico: String?
func stampRow()->String{
let v1 = String(id) + " " + String(cliente) + " "
let v2 = dataInizio! + " " + dataFine! + " "
let v3 = stato! + " " + tecnico! + " " + descrizione!
return v1 + v2 + v3
}
}
and this is the my code for save the data (it works)
#IBAction func addButton(_ sender: Any){
//genero la data
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let strDate = dateFormatter.string(from: myDatePicker.date)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managerContext = appDelegate.persistentContainer.viewContext
let newInt = NSEntityDescription.insertNewObject(forEntityName: "Intervento", into: managerContext)
newInt.setValue(1, forKey: "id")
newInt.setValue(strDate, forKey: "dataInizio")
newInt.setValue("", forKey: "dataFine")
newInt.setValue(2, forKey: "cliente")
newInt.setValue("Matt", forKey: "tecnico")
newInt.setValue(stato[1], forKey: "stato")
newInt.setValue("", forKey: "descrizione")
newInt.setValue("", forKey: "foto")
do{
try managerContext.save()
print("saved")
}catch let error as NSError{
//errore salvataggio
print("Could not fetch \(error), \(error.userInfo)")
}
}

First of all, your class should implement this protocols:
UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate
You can load data from a persistent storage with this code:
//load data
//persistant container
let persistentContainer = NSPersistentContainer.init(name: "Model")
//
lazy var fetchedResultsController: NSFetchedResultsController<Intervento> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Intervento> = Intervento.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dataInizio", ascending: false)]
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managerContext = appDelegate.persistentContainer.viewContext
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managerContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
//carica
func load(){
//persistant container
persistentContainer.loadPersistentStores { (persistentStoreDescription, error) in
if let error = error {
print("Unable to Load Persistent Store")
print("\(error), \(error.localizedDescription)")
} else {
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("Unable to Perform Fetch Request")
print("\(fetchError), \(fetchError.localizedDescription)")
}
}
}
}
override func viewDidLoad(){
super.viewDidLoad()
iTableView.delegate = self
iTableView.dataSource = self
// trigger load
load()
}
Your data will be fetched and will show up because you have delegated iTableViewController by this line of code:
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
In order to make this work, your ViewController must conform to NSFetchedResultsControllerDelegate protocol, here you can find a possibile implementation:
extension iTableViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
iTableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
iTableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
iTableView.insertRows(at: [indexPath], with: .fade)
}
break;
case .delete:
if let indexPath = indexPath {
iTableView.deleteRows(at: [indexPath], with: .fade)
}
break;
case .update:
if let indexPath = indexPath, let cell = iTableView.cellForRow(at: indexPath) {
configureCell(cell, at: indexPath)
}
break;
case .move:
if let indexPath = indexPath {
iTableView.deleteRows(at: [indexPath], with: .fade)
}
if let newIndexPath = newIndexPath {
iTableView.insertRows(at: [newIndexPath], with: .fade)
}
break;
}
}
}
In conclusion, this is the full implementation of your iTableViewController (I have included also the necessary code for deleting an row):
class iTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet var iTableView: UITableView!
//load data
//persistant container
let persistentContainer = NSPersistentContainer.init(name: "Model")
//
lazy var fetchedResultsController: NSFetchedResultsController<Intervento> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Intervento> = Intervento.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dataInizio", ascending: false)]
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managerContext = appDelegate.persistentContainer.viewContext
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managerContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
//carica
func load(){
//persistant container
persistentContainer.loadPersistentStores { (persistentStoreDescription, error) in
if let error = error {
print("Unable to Load Persistent Store")
print("\(error), \(error.localizedDescription)")
} else {
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("Unable to Perform Fetch Request")
print("\(fetchError), \(fetchError.localizedDescription)")
}
}
}
}
override func viewDidLoad(){
super.viewDidLoad()
iTableView.delegate = self
iTableView.dataSource = self
// trigger load
load()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//numero di sezioni
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//numero di righe
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let quotes = fetchedResultsController.fetchedObjects else { return 0 }
return quotes.count
}
//gestisco la cella
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//genero la cella
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
configureCell(cell, at:indexPath)
return cell
}
//swipe and delete
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if(editingStyle == .delete){
// Fetch Quote
let quote = fetchedResultsController.object(at: indexPath)
// Delete Quote
quote.managedObjectContext?.delete(quote)
}
}
// click cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
performSegue(withIdentifier: "segueDetail", sender: self)
}
func configureCell(_ cell: UITableViewCell, at indexPath: IndexPath) {
//get intervento
let intervento = fetchedResultsController.object(at: indexPath)
//fill the cell
cell.textLabel?.text = intervento.dataInizio
cell.detailTextLabel?.text = "SOME_THING"
}
//quando appare aggiorno la table view
override func viewDidAppear(_ animated: Bool){
self.iTableView.reloadData()
}
}
extension iTableViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
iTableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
iTableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
iTableView.insertRows(at: [indexPath], with: .fade)
}
break;
case .delete:
if let indexPath = indexPath {
iTableView.deleteRows(at: [indexPath], with: .fade)
}
break;
case .update:
if let indexPath = indexPath, let cell = iTableView.cellForRow(at: indexPath) {
configureCell(cell, at: indexPath)
}
break;
case .move:
if let indexPath = indexPath {
iTableView.deleteRows(at: [indexPath], with: .fade)
}
if let newIndexPath = newIndexPath {
iTableView.insertRows(at: [newIndexPath], with: .fade)
}
break;
}
}
}
Don't forget to use always the same context when you make operations with your data (for example when you add new data).
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managerContext = appDelegate.persistentContainer.viewContext
For a full example you should look here: https://github.com/bartjacobs/ExploringTheFetchedResultsControllerDelegateProtocol

Swift 3.3
lazy var fetchedResultsController: NSFetchedResultsController<SavedContact> = {
let fetchReqest: NSFetchRequest<SavedContact> = SavedContact.fetchRequest()
fetchReqest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchReqest, managedObjectContext: Database.shared.mainManagedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
override func viewDidLoad() {
super.viewDidLoad()
self.performFetch()
}
func performFetch() {
do {
try self.fetchedResultsController.performFetch()
} catch {
Debug.Log(message: "Error in fetching Contact \(error)")
}
}
//MARK: - NSFetchResultcontrollerDelegate
extension YourViewC: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.reloadData()
}
}
// Database Class
private let _singletonInstance: Database = Database()
class Database: NSObject {
class var shared: Database {
return _singletonInstance
}
override init() {
super.init()
}
// Main Managed Object Context
lazy var mainManagedObjectContext: NSManagedObjectContext = {
let coordinator = self.persistentStoreCoordinator
var mainManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
mainManagedObjectContext.persistentStoreCoordinator = coordinator
return mainManagedObjectContext
}()
// Persistent Store Coordinator
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.appendingPathComponent("AppName.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: mOptions)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
abort()
}
return coordinator
}()
// DB Directory and Path
lazy var applicationDocumentsDirectory: URL = {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectoryURL = urls[urls.count - 1] as URL
let dbDirectoryURL = documentDirectoryURL.appendingPathComponent("DB")
if FileManager.default.fileExists(atPath: dbDirectoryURL.path) == false{
do{
try FileManager.default.createDirectory(at: dbDirectoryURL, withIntermediateDirectories: false, attributes: nil)
}catch{
}
}
return dbDirectoryURL
}()
}

Related

Swift: UITableViewController selecting cell & passing fetched objects to UIViewController

I’m trying to make a simple note app using Core Data, but I’m running into problem with fetchedResultsController.object(at: indexPath) & passing the data onto my View Controller. using prepare(for segue: UIStoryboardSegue, sender: Any?).
My Core Data Entity is named Notes with the following Attributes
dateStamp Integer 64
noteName String
noteImage Data
noteDescription String
However in order to understand the problem I’ve made separate project & limited it to just the noteName & dateStamp.
So there are three Controllers & one Helper file
MasterViewController
AddViewController
DetailViewController
EditViewController
dateHelper
The MasterView is my UITableController with my NSFetchedResultsControllerDelegate, so the code is as follows….
import UIKit
import CoreData
class MasterViewController: UITableViewController {
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
var fetchedResultsController: NSFetchedResultsController<Notes>!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
fetchFromCoreData()
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
}
#objc
func insertNewObject(_ sender: Any) {
let addController = storyboard?.instantiateViewController(withIdentifier: "addNotes")
as! UINavigationController
self.present(addController, animated: true, completion: nil)
}
private func configureCells(cell: noterViewCell, withEvent note: Notes, indexPath: IndexPath) {
let record = fetchedResultsController.object(at: indexPath)
cell.noteName.text = record.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: record.dateStamp))
if let noteName = record.value(forKey: "noteName") as? String, let dateTime = record.value(forKey: "dateStamp") as? Int64 {
cell.noteName.text = noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: dateTime))
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController.sections?.count ?? 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sections = self.fetchedResultsController?.sections else {
fatalError("No sections in fetchedResultsController")
}
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "noteCell", for: indexPath) as! noterViewCell
guard let object = self.fetchedResultsController?.object(at: indexPath) else {
fatalError("Attempt to configure cell without a managed object")
}
configureCells(cell: cell, withEvent: object, indexPath: indexPath)
cell.selectbutton.addTarget(self, action: #selector(selectNote(_:)), for: .touchUpInside)
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let context = fetchedResultsController.managedObjectContext
context.delete(fetchedResultsController.object(at: indexPath))
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func fetchFromCoreData() {
let fetchRequest: NSFetchRequest<Notes> = Notes.fetchRequest()
fetchRequest.fetchBatchSize = 800
let creationDateSortDescriptor = NSSortDescriptor(key: "dateStamp", ascending: false)
//let nameSortDescriptor = NSSortDescriptor(key: "noteName", ascending: false)
fetchRequest.sortDescriptors = [creationDateSortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "dateStamp", cacheName: nil)
aFetchedResultsController.delegate = self
fetchedResultsController = aFetchedResultsController
do {
try fetchedResultsController.performFetch()
self.tableView.reloadData()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.localizedDescription), \(nserror.localizedFailureReason ?? "could not retrieve")")
//print("Could not save note to CoreData: \(error.localizedDescription)")
}
}
}
extension MasterViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
return
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update:
tableView.reloadRows(at: [newIndexPath!], with: .fade)
if let updateIndexPath = newIndexPath {
let cell = self.tableView.cellForRow(at: updateIndexPath) as! noterViewCell
let event = anObject as! Notes
cell.dateLabel.text = event.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
}
case .move:
configureCells(cell: tableView.cellForRow(at: indexPath!) as! noterViewCell, withEvent: anObject as! Notes, indexPath: indexPath!)
tableView.moveRow(at: indexPath!, to: newIndexPath!)
#unknown default:
fatalError("Unresolved error")
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
}
My AddViewController
import UIKit
import CoreData
class AddViewController: UIViewController {
#IBOutlet var noteField: UITextField!
#IBOutlet var dateStamp: UILabel!
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
let masterView = MasterViewController()
var isExisting: Bool = false
var note:Notes? = nil
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
var detailItem: Notes? {
didSet {
// Update the view.
configureView()
}
}
func configureView() {
if let detail = detailItem {
if let noteFieldLabel = noteField, let dateTime = dateStamp {
noteFieldLabel.text = detail.noteName
dateTime.text = dateHelper.convertDate(date: Date.init(seconds: detail.dateStamp))
}
}
}
#IBAction func saveImageButtonPressed(_ sender: Any) {
let context = self.managedObjectContext
let newEvent = Notes(context: context!)
newEvent.noteName = noteField.text
newEvent.dateStamp = noteCreationTimeStamp
do {
try context?.save()
} catch {
let error = error as NSError
fatalError("Unresolved error \(error), \(error.localizedDescription)")
}
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
else {
self.navigationController!.pushViewController(masterView, animated: true)
}
}
#IBAction func cancelView(_ sender: AnyObject) {
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
}
}
Everything is good there my saveImageButtonPressed saves the data to my managedObjectContext and displays onto the UITableview with no problems.
This code for my DetailViewController
import UIKit
import CoreData
class DetailViewController: UIViewController {
#IBOutlet var dateLabel: UILabel!
#IBOutlet var noteField: UILabel!
let masterView = MasterViewController()
var notes: Notes?
var myNotes = [Notes]()
var isExisting: Bool = false
var index = IndexPath()
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
var detailItem: Notes? {
didSet {
// Update the view.
configureView()
}
}
func configureView() {
if let detail = detailItem {
if let noteFieldLabel = noteField, let dateStamp = dateLabel {
noteFieldLabel.text = detail.noteName
dateStamp.text = dateHelper.convertDate(date: Date.init(seconds: detail.dateStamp))
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//fetchNote()
configureView()
}
#IBAction func cancelView(_ sender: AnyObject) {
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
}
//
func fetchNote() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Notes")
do {
myNotes = try managedObjectContext?.fetch(fetchRequest) as! [Notes]
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.localizedDescription,\(nserror.localizedFailureReason ?? "could not retrieve")")
}
}
//
}
So I’m trying to select my cell to pass the saved data to my DetailViewController but it is not selecting. Nothing happens. The fetchNote function in my DetailViewController was just add-on to try out.
This is my code for my prepare(for segue: UIStoryboardSegue, sender: Any?)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
let objects = fetchedResultsController.object(at: selectedIndexPath)
controller.detailItem = objects
}
}
}
and my tableView.didSelectRowAt: indexPath
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = self.tableView.cellForRow(at: indexPath) as! noterViewCell
let event = fetchedResultsController.object(at: indexPath)
cell.dateLabel.text = event.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
//configureCells(cell: tableView.cellForRow(at: indexPath!) as! noterViewCell, withEvent: anObject as! Notes, indexPath: indexPath!)
self.performSegue(withIdentifier: "showDetails", sender: self)
}
I added self.performSegue(withIdentifier: "showDetails", sender: self) but the DetailViewController is just blank.
In order to understand further I added button in the cellForRowAt indexPath:…
cell.selectbutton.addTarget(self, action: #selector(selectNote(_:)), for: .touchUpInside)
with the following action
#objc
func selectNote(_ sender: noterViewCell) {
let indexPath = IndexPath()
let objects = fetchedResultsController.object(at: indexPath) as Notes
let controller = DetailViewController()
controller.detailItem = objects
//let detailView = //storyboard?.instantiateViewController(withIdentifier: "viewNotes") as! //UINavigationController
//self.present(detailView, animated: true, completion: nil)
let detailViews = DetailViewController()
self.navigationController?.popToViewController(detailViews, animated: true)
}
and this threw an 'NSInvalidArgumentException', reason: 'no section at index 9223372036854775807' with both methods tried in accessing my Detail View
This was directed exactly on fetchedResultsController.object in my button action #objc MasterViewController_selectNote(_:)
Also listed in the thread was [NSFetchedResultsController objectAtIndexPath:]: scrolling down I found another reason, "cannot access fetched objects before -performFetch:" as well as "NSFetchedResultsController: no object at index %lu in section at index %lu"
I’m calling my fetchFromCoreData() in my viewDidLoad() … I took out the performFetch I pasted it directly in the viewDidLoad(). Still throws the same error if I click on the button. Again blank view selecting using prepareForSegue. I tried using return value in my fetchFromCoreData() function again the same.
I’m not sure if I added to the problem by adding the button but in all cases I’ve attempted to remedy the problem prepareForSegue its still passing a blank view.
I also tried to validate the indexPath using this function
func validateIndexPath(_ indexPath: IndexPath) -> Bool {
if let sections = self.fetchedResultsController.sections,
indexPath.section < sections.count {
if indexPath.row < sections[indexPath.section].numberOfObjects {
return true
}
}
return false
}
again to no avail, calling it on the cellForRowAt indexPath & the app still terminates with the same 'NSInvalidArgumentException’ and the prepareForSegue passing blank view.
Then I added my titleForHeaderInSection in my tableView which is set by month & year that is displaying okay when I save the data in my AddViewController. I hoped it might make a bit of difference but…
func monthDate(string:String?) -> String? {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM"
let dateString = formatter.string(from: date)
let monthDate = formatter.date(from: dateString)
formatter.dateFormat = "MMM, yyyy"
let dateMonth = formatter.string(from: monthDate!)
return dateMonth
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let sectionInfo = fetchedResultsController.sections?[section] else {
return nil
}
return monthDate(string: sectionInfo.name)
}
I really just don't understand where I'm going wrong... I’m using iPhone 11 pro max simulator on IOS14 & Xcode 12.3.(I upgraded about month ago & it did hang on install for about 4 or 5 days, its been bit buggy since)
Although I’m sure I’ve probably missed something quite simple any help will be much appreciated.
On the other hand if I've understood correctly I’m aware NSFetchedResultsController does not like empty sections as per this blog post…
http://www.iosnomad.com/blog/2014/8/6/swift-nsfetchedresultscontroller-trickery
and if that could be the problem, is there simpler solution for Swift 5?
EDIT
In my tableview didSelectRowAt I added
print(fetchedResultsController.object(at: indexPath))
and the console outputs my saved data but it's still not passing it to my DetailViewController.
I tried also in my prepareForSegue adding
print(fetchedResultsController.object(at: selectedIndexPath))
This returned no saved data, as well as returning 0 sections after printing out on the console the numberOfSections in tableView, after selecting my cell. But as I open my app the data is still saved and viewable in my cell and prints out correct section numbers in the console, as I add new note. Only when I select my cell to pass saved data to my DetailViewController the problem occurs.
Problem Solved
I added following variables to my UITableViewController
var cellLabel: [Notes] = []
var selectedLabels = String()
var selectedTime = String()
and added variables to my DetailViewController
var receivedString: String = ""
var recievedTime: String = ""
I changed my code to reflect fetched objects are attached to my selected variables in my didSelectRowAt and equal to my received variables in my prepareForSegue.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let event = fetchedResultsController.object(at: indexPath)
cellLabel = [event]
selectedLabels = event.noteName!
selectedTime = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
print(selectedLabels)
print(selectedTime)
self.performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.receivedString = selectedLabels
controller.recievedTime = selectedTime
print(selectedLabels)
print(selectedTime)
/*
if let indexPath = self.tableView.indexPathForSelectedRow {
let objects = fetchedResultsController.object(at: indexPath)
controller.noteField.text = objects.noteName
controller.detailItem = objects
}
*/
}
}
and in my viewDidLoad() in DetailViewController
override func viewDidLoad() {
super.viewDidLoad()
noteField.text = receivedString
dateLabel.text = recievedTime
}

Getting user input to copy from a TableViewController to a ViewController using Swift

I'm working on an app in which the user inputs three values (artist, album and release date). I'm trying to get those user entered values to 'copy' over to another TableViewController.
The values show on this, the FreshReleaseTableViewController (the code below) and I need them to copy to EditFreshReleaseViewController.
How would I go about this?
import UIKit
import CoreData
import UserNotifications
class FreshReleaseTableViewController: UITableViewController{
var freshreleases = [Release_Date]()
let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
//create a new button
let button = UIButton.init(type: .custom)
//set image for button
button.setImage(UIImage(named: "Mic App Logo.png"), for: UIControlState.normal)
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .none
}
#objc func editAction() {
let viewController = AddfreshreleaseViewController()
navigationController?.present(viewController, animated: true, completion: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = Release_Date.fetchRequest() as NSFetchRequest<Release_Date>
let sortDescriptor1 = NSSortDescriptor(key: "artist", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "album", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]
do {
freshreleases = try context.fetch(fetchRequest)
} catch let error {
print("Could not fetch because of error: \(error).")
}
/*let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %#", startOfToday)
fetchRequest.predicate = predicate*/
NSFetchedResultsController<NSFetchRequestResult>()
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return freshreleases.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FreshReleaseCellIdentifier", for: indexPath)
let freshrelease = freshreleases[indexPath.row]
cell.textLabel?.numberOfLines = 0
let artist = freshrelease.artist ?? ""
let album = freshrelease.album ?? ""
cell.textLabel?.text = artist + "'s\nnew album '" + album + "'\nreleases"
if let date = freshrelease.release_date as Date? {
cell.detailTextLabel?.text = dateFormatter.string(from: date)
} else {
cell.detailTextLabel?.text = ""
}
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if freshreleases.count > indexPath.row {
let freshrelease = freshreleases[indexPath.row]
// Remove notification
if let identifier = freshrelease.release_dateId {
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: [identifier])
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.delete(freshrelease)
freshreleases.remove(at: indexPath.row)
do {
try context.save()
} catch let error {
print("Could not save \(error)")
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
#available(iOS 11.0, *)
override func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
let MainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc : UIViewController = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as UIViewController
self.present(vc, animated: true, completion: nil)
success(true)
})
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
}
Add to your EditFreshReleaseViewController variable which represents selected fresh release
var selectedFreshRelease: Release_Date?
now in line where you're declaring view controller which is gonna be presented, downcast this controller as EditFreshReleaseViewController
let vc = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as! EditFreshReleaseViewController
then just assign its variable as item from freshReleases array on index indexPath.row
vc.selectedFreshRelease = self.freshReleases[indexPath.row]
finally, present this view controller as you do
self.present(vc, animated: true, completion: nil)

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value when using SearchController

Ive been working on my own version of an app from appCoda. I input the search bar the same way in the app but when I build and run the app I get an error at the searchController.searchResultsUpdater = self. When I comment that out the same error goes to, searchController.dimsBackgroundDuringPresentation = false.
import UIKit
import CoreData
class HousesTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UISearchResultsUpdating {
var homes: [HomeMO] = []
var searchResults: [HomeMO] = []
var fetchResultController : NSFetchedResultsController<HomeMO>!
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
//Error is here↓
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = searchController
navigationController?.navigationBar.tintColor = UIColor(red: 94, green: 252, blue: 161)
if let customFont = UIFont(name: "Rubik-Medium", size: 40.0) {
navigationController?.navigationBar.largeTitleTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: customFont ]
}
let fetchRequest: NSFetchRequest<HomeMO> = HomeMO.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "price", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate){
let context = (appDelegate.persistentContainer.viewContext)
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do{
try fetchResultController.performFetch()
if let fetchedObjects = fetchResultController.fetchedObjects{
homes = fetchedObjects
}
} catch {
print(error)
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searchController.isActive{
return searchResults.count
}else {
return homes.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as! HomesTableViewCell
let home = (searchController.isActive) ? searchResults[indexPath.row] : homes[indexPath.row]
cell.priceLabel.text = "$" + home.price!
if let homeImage = home.image{
cell.thumbnailImageView.image = UIImage(data: homeImage as Data)
}
cell.amountOfBathsLabel.text = (home.amountOfBaths! + "ba")
cell.amountOfBedsLabel.text = (home.amountOfBeds! + "bds")
cell.locationLabel.text = home.location?.uppercased()
cell.amountOfSquareFeetLabel.text = (home.amountOfSquareFeet! + "sqft")
cell.typeLabel.text = home.type
cell.layer.cornerRadius = 20
cell.layer.masksToBounds = true
// Configure the cell...
return cell
}
func filterContent(for searchText: String) {
searchResults = homes.filter({ (home) -> Bool in
if let type = home.type {
let isMatch = type.localizedCaseInsensitiveContains(searchText)
return isMatch
}
return false
})
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text{
filterContent(for: searchText)
tableView.reloadData()
}
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 61.0
}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .fade)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
}
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .fade)
}
default:
tableView.reloadData()
}
if let fetchedObjects = controller.fetchedObjects {
homes = fetchedObjects as! [HomeMO]
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
#IBAction func unwindToHome(segue: UIStoryboardSegue){
dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showHomeDetail"{
if let indexPath = tableView.indexPathForSelectedRow{
let destinationController = segue.destination as! HomeDetailViewController
destinationController.home = searchController.isActive ? searchResults[indexPath.row] : homes[indexPath.row]
}
}
}
}
Look at your code. The controller is initialized after setting the delegate. Always initialize an object before using it.
So change the order
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.navigationItem.searchController = searchController

Core Data: Showing NSFetchRequestResult Search Results with UITableView

I've been playing Core Data for the past several days. I'm fetching data with NSFetchedResultsController. My entity has such attributes as age (Int), firstName (String), lastName (String), uuid (String). I am able to insert a new record to the database. I am able to delete a record. I am also able to edit a record. What I cannot do is to show a search result with the table.
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: - Instance variables
private let persistentContainer = NSPersistentContainer(name: "Profiles") // core data model file (.xcdatamodeld)
var managedObjectContext: NSManagedObjectContext?
// MARK: - IBOutlets
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var searchButton: UIButton!
// MARK: - IBActions
#IBAction func searchTapped(_ sender: UIButton) {
searchRecords()
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// loading persistentContainer //
persistentContainer.loadPersistentStores { (persistentStoreDescription, error) in
if let error = error {
print("Unable to Load Persistent Store")
print("\(error), \(error.localizedDescription)")
} else {
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("Unable to Perform Fetch Request")
print("\(fetchError), \(fetchError.localizedDescription)")
}
}
}
}
// MARK: - Life cycle
// MARK: - fetchedResultsController(controller with the entity)
fileprivate lazy var fetchedResultsController: NSFetchedResultsController<Person> = {
// Create Fetch Request with Entity
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastName", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
// MARK: - fetchedResultsController
// MARK: - TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let people = fetchedResultsController.fetchedObjects else { return 0 }
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProfileTableViewCell
let person = fetchedResultsController.object(at: indexPath)
// Configure Cell
cell.firstLabel.text = person.firstName
cell.lastLabel.text = person.lastName
cell.ageLabel.text = String(person.age)
return cell
}
// MARK: - TableView
// MARK: - Showing search result
func searchRecords() {
let context = self.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
let predicate = NSPredicate(format: "firstName CONTAINS[c] %#", "Sandra")
fetchRequest.predicate = predicate
do {
try fetchedResultsController.performFetch()
/*
let result = try context.fetch(fetchRequest)
if (result.count > 0) {
print(result.count)
}
*/
} catch {
print("bad")
}
}
// MARK: - Showing search result
}
extension HomeViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
tableView.insertRows(at: [indexPath], with: .fade)
}
break;
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
}
break;
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .automatic)
}
break;
default:
tableView.reloadData()
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
}
}
If I run searchRecords(), result.count will correctly return the number of records. But the table stays the same. So how can I show my search result with the table? Thanks.
Replace your tableview datasource with
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
Hope it is helpful
You've got more experience than I do, but the last time I used CoreData and a collectionView I needed to force cast the result like this:
do {
return try context.fetch(request) as? [Friend]
} catch let err {
print(err)
}
Then I was able to pull the info to the table more easily.

How can I fetch core data objects into an array

I am trying to build an application that helps dyslectic children to automate words and I cannot get the lists in my app to work as desired: users can create lists [LijstMO] that contain a set of words [WoordMO]. I am able to create and fetch lists, and to create words, but I can't create an army of words that belong to a specific list. Which superhero can make my day and help me out with a filtered fetch request? All help is extremely appreciated.
First my core data model:
my data model
Then my code:
import UIKit
import CoreData
class WoordTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var woordTextField: UITextField!
var lijst: LijstMO!
var woord: WoordMO!
var woordeninlijst: [WoordMO] = []
var fetchResultController: NSFetchedResultsController<WoordMO>!
override func viewDidLoad() {
super.viewDidLoad()
title = lijst.naam
self.woordTextField.delegate = self
let fetchRequest: NSFetchRequest<WoordMO> = WoordMO.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "naam", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
if let fetchedObjects = fetchResultController.fetchedObjects {
woordeninlijst = fetchedObjects
}
} catch {
print(error)
}
}
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// foutmelding leeg woord
if woordTextField.text == "" {
let alertController = UIAlertController(title: "Oooo Neeeee", message: "Zonder tekst kunnen we dit woord echt niet opslaan. Geef 'm een mooie naam en druk op Bewaar. Ka-tching! ", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Okidoki", style: .default, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
return false
}
else {
print("Woord: \(woordTextField.text ?? "")")
// saving the restaurant to database
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
// create new list
woord = WoordMO(context: appDelegate.persistentContainer.viewContext)
woord.naam = woordTextField.text
woord.zitInLijst = lijst
woordTextField.text = ""
appDelegate.saveContext()
print ("WoordMO: \(woord.naam ?? "")")
}
return true
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return woordeninlijst.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! WoordTableViewCell
cell.woordLabel.text = woordeninlijst[indexPath.row].naam
return cell
}
// MARK: NSFetchedResultsControllerDelegate methods
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .fade)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
}
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .fade)
}
default:
tableView.reloadData()
}
if let fetchedObjects = controller.fetchedObjects {
woordeninlijst = fetchedObjects as! [WoordMO]
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}