Amateur User
Amateur User

Reputation: 91

How to delete objects from the Core Data?

In the iOS Notes App you can see UITableView's cells! When you tapped the cell, and cell is passing data to the another UIViewController (for example: DetailViewController )! And inside of the DetailViewController you can delete this data from the Database!

So, I have an app with UITableView, Core Data and NSFetchedResultsController as well! I can delete data from the UITableView using default swipe to delete function! But I don't understand, how to delete them from the DetailViewController. If it's passed to the DetailViewController. If you have any suggestions please tell me. I'm very new to the iOS Development and Swift Programming Language as well.

My DetailViewController!

import UIKit

import CoreData

class DetailViewController: UIViewController {

@IBOutlet weak var containerLabel: UILabel!

var retrieveData:NSManagedObject!
var managedObjectContext:NSManagedObjectContext!

override func viewDidLoad() {
    super.viewDidLoad()
// Do any additional setup after loading the view.

if let demo = self.retrieveData.valueForKey("titleField") as? String {
     self.containerLabel.text = demo
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func backToMain(sender: AnyObject) {
    // Back to the MainTableViewController
    self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func trashButton(sender: AnyObject) {

    self.managedObjectContext.deleteObject(retrieveData)

    do {
    try self.managedObjectContext.save()
    } catch {

    }
    self.dismissViewControllerAnimated(true, completion: nil)


}

Upvotes: 1

Views: 1339

Answers (1)

Tuslareb
Tuslareb

Reputation: 1200

You probably passed your managed object into the detail viewController. So let's say you have a reference to this managed object in the detail viewController called 'managedObject'. Let's also assume you have a reference to the NSManagedObjectContext called 'context'.

The code to delete this object and to save the context is:

context.deleteObject(managedObject)
do {
    try context.save()
    }
    catch let error{
    NSLog("Unresolved error while saving managedObjectContext: \(error)")
 }

After the delete, you of course need some code to update your user interface. The notes app shows the next note in the row, but you can also unwind to the UITableView in the mainscreen if you want.

The UITableView controller has to adopt the NSFetchedResultsControllerDelegate protocol by implementing some delegate methods that will update the tableView. You deleted an object, so the tableView has to be notified so it can delete the row the object was in. You can find a lot of information here or elsewhere on the web how to do that. Check the protocol reference for NSFetchedResultsControllerDelegate.

Edit: if you want to pass over your context and object you will need something like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "yourSegueIdentifier"{

        if let destinationVC = segue.destinationViewController as? YourCustomVC{

       destinationVC.context = yourContextObject
       destinationVC.managedObject = yourManagedObject
       //pass over other data...

         }
     }
}

After passing over the relevant objects, your can use the given code to delete a managedObject from the context. Hope this helps.

Upvotes: 2

Related Questions