Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Data from Coredata Swift

In my tableViewController I have the following. And I am trying to get delete an item to work.

var myData: Array<AnyObject> = []  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cellID: NSString = "Cell"     var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell     var data: NSManagedObject = myData[indexPath.row] as NSManagedObject     Cell.textLabel?.text = data.valueForKeyPath("Name") as? String      return Cell } 

Then to try and delete I have.

override func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {     if editingStyle == .Delete {         let cellID: NSString = "Cell"         var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell         var data: NSManagedObject = myData[indexPath.row] as NSManagedObject         data.delete(0)          // Delete the row from the data source         //tableView!.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)       } else if editingStyle == .Insert {         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view     } } 
like image 482
MwcsMac Avatar asked Sep 25 '14 19:09

MwcsMac


People also ask

How delete all data from Core Data?

One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

How do I save in Core Data?

Right-click on your project's folder in the project navigator and then New File… In the new window, type “data” in the top right corner, select Data Model, and press Next. Give it a name, and save it. Now, let's add all the necessary code to connect Core Data with our project.

Should I use Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.


1 Answers

Update on my coding issue with executing a delete of data in swift and coredata. This the code I ended up with that worked.

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {         switch editingStyle {         case .Delete:             // remove the deleted item from the model             let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate             let context:NSManagedObjectContext = appDel.managedObjectContext!             context.deleteObject(myData[indexPath.row] as NSManagedObject)             myData.removeAtIndex(indexPath.row)             context.save(nil)             //tableView.reloadData()             // remove the deleted item from the `UITableView`             self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)         default:             return          } } 

EDIT Above for Swift 2.2 and Xcode 7.3.1

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {     switch editingStyle {     case .Delete:         // remove the deleted item from the model         let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate         let context:NSManagedObjectContext = appDel.managedObjectContext         context.deleteObject(myData[indexPath.row] )         myData.removeAtIndex(indexPath.row)         do {             try context.save()         } catch _ {         }          // remove the deleted item from the `UITableView`         self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)     default:         return     } } 

Also need was these two lines of code to be corrected.

    var myData: Array<AnyObject> = [] let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext 
like image 56
MwcsMac Avatar answered Oct 02 '22 10:10

MwcsMac