how to delete row from coredata (Entity) ios swift - ios

i am new to CORE DATA in my app i am using coredata.
i just stored data in my core data. my entity name is "FEED" and i have a rows with name "title", "id " , "link" ,"desc" so now i want to delete perticular row on based on "id". so how can i do this?
Here is my code,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell
cell.pinButton.tag = indexPath.row
cell.pinButton.addTarget(self, action: #selector(MessageViewController.buttonDeletePressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
cell.selectionStyle = .None
person = people[indexPath.row]
cell.lbl_title_msg_music.text = person!.valueForKey("title") as? String
cell.img_message_music.image = UIImage(named: "PlaceHolder")
cell.desc_msg_music.text = person!.valueForKey("desc") as? String
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(people.count)
return people.count
}
func buttonDeletePressed(sender:UIButton) {
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell
}
so how can i do this?

Try Like this,
func buttonDeletePressed(sender:UIButton) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let index = sender.tag
context.deleteObject(people[index] as NSManagedObject)
people.removeAtIndex(index)
let _ : NSError! = nil
do {
try context.save()
self.tableView.reloadData()
} catch {
print("error : \(error)")
}
}
Hope this will help you.

func deleteFeed(id:String)
{
let appDelegate =
UIApplication.sharedApplication().delegate as? AppDelegate
let managedContext = appDelegate?.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"FEED")
fetchRequest.predicate = NSPredicate(format: "id = %#", "\(id)")
do
{
let fetchedResults = try managedContext!.executeFetchRequest(fetchRequest) as? [NSManagedObject]
for entity in fetchedResults! {
managedContext?.deleteObject(entity)
}
}
catch _ {
print("Could not delete")
}
}
Now you can call this function and pass the id whatever you want to delete
func buttonDeletePressed(sender:UIButton){
deleteFeed("1")
}

You can delete data like,
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)
this is done from commitEditingStyle so here used indexPath.row. You can pass your id if you want to delete data from other place. If you want to delete data from tableview then you should implement commitEditingStyle and delete data like mentioned above.
Hope this will help :)

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(people[indexPath.row] )
people.removeAtIndex(indexPath.row)
do {
try context.save()
} catch _ {
}
// remove the deleted item from the `UITableView`
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
return
}
}
Hope this could be of some help!

Related

Tableview cells not showing up correctly, and core data not loading the saved data correctly

in my app I am trying to create a budgeting app. I have tried creating a custom tableView Cell, but when it is run in the simulator, the tableView is condensed and does not show up correctly. As well as this, I am trying to save and load data from Coredata. However when the viewController loads, the error "Fatal Error: Index Out of Range" appears. I have viewed many coreData tutorials and threads however none of them help with my issue.
I want my tableViews to show up like this: storyboard view
However they appear like this.
Here is my code for the tableView and core data.
Any help is appreciated, thank you!
ViewController
var goals: [NSManagedObject] = []
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(goals.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllGoalsTableViewCell
let goal = goals[1]
cell.titleLabel?.text = goal.value(forKey: "title") as? String
return cell
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//1
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
//2
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Goal")
//3
do {
goals = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
if !self.goals.isEmpty
{
tableView.reloadData()
}
}
addNewViewController
var goals: [NSManagedObject] = []
func save(title: String, goalAmount: Double, percent: Double, amountPerWeek: Double) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
//1
let managedContext = appDelegate.persistentContainer.viewContext
//2
let entity = NSEntityDescription.entity(forEntityName: "Goal", in: managedContext)!
let goal = NSManagedObject(entity: entity, insertInto: managedContext)
//3
goal.setValue(title, forKey: "title")
goal.setValue(goalAmount, forKey: "goalAmount")
goal.setValue(percent, forKey: "percent")
goal.setValue(amountPerWeek, forKey: "amountPerWeek")
//4
do {
try managedContext.save()
goals.append(goal)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
#IBAction func addNewAction(_ sender: Any)
//CoreData
self.save(title: title, goalAmount: goalAmount ?? 0.0, percent: percent, amountPerWeek: amountPerWeekFloat ?? 0.0)
print("saved")
}

Error in CoreData when trying to save an array. 'Cannot convert value of type 'String' to expected argument type 'NSManagedObject''

I am using CoreData to save a tableView to the device. I am relatively new to CoreData, and cannot figure this out. I get the error:
'Cannot convert value of type 'String' to expected argument type 'NSManagedObject''
On the line:
favourites.append(addNewMemory.text!)
//MARK:- Core Data
func save(name: String) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
// 1
let managedContext =
appDelegate.persistentContainer.viewContext
// 2
let entity =
NSEntityDescription.entity(forEntityName: "Memory",
in: managedContext)!
let person = NSManagedObject(entity: entity,
insertInto: managedContext)
// 3
person.setValue(name, forKeyPath: "name")
// 4
do {
try managedContext.save()
favourites.append(person)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
var favourites: [NSManagedObject] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favourites.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell")
/*cell.imageView?.image = UIImage(named: "applelogo")
cell.imageView?.setRounded()
cell.imageView?.clipsToBounds = true
*/
let favMemory = favourites[indexPath.row]
cell.textLabel?.text = favMemory.value(forKeyPath: "name") as? String
return cell
}
#IBAction func addButtonTapped(_ sender: UIButton) {
insertNewCell()
}
func insertNewCell() {
favourites.append(addNewMemory.text!)
let indexPath = IndexPath(row: favourites.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
addNewMemory.text = ""
view.endEditing(true)
}
I expected the app to save the string, but it does not work. How can I fix this?
You are mixing up the text property of the text field and the Core Data entity. Obviously favourites is declared as [NSManagedObject] so you can't append a string. That's what the error message is telling you.
You have to insert a new record in insertNewCell. The easiest solution is to call save and return a Bool from save to indicate that the insertion was successful.
And you are encouraged to use more contemporary API. If there is no Memory subclass create one
var favourites = [Memory]()
...
func save(name: String) -> Bool {
let appDelegate = UIApplication.shared.delegate as! AppDelegate // force unwrapping is perfectly fine
// 1
let managedContext = appDelegate.persistentContainer.viewContext
// 2
let person = Memory(context: managedContext)
// 3
person.name = name
// 4
do {
try managedContext.save()
favourites.append(person)
return true
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
return false
}
}
and change insertNewCell to
func insertNewCell() {
guard save(name: addNewMemory.text!) else { return }
let indexPath = IndexPath(row: favourites.count - 1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
addNewMemory.text = ""
view.endEditing(true)
}
beginUpdates/endUpdates is pointless.

reload data from core data in swift

I'm trying to fetch data from core data with table view cells, but when the data is reloaded each item in the cell take separate cells, I hope you understand what I mean.
this is the code that I think it has an error
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "ShopList")
//3
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
people = results as! [NSManagedObject]
let results2 =
try managedContext.executeFetchRequest(fetchRequest)
ids = results2 as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! itemsList
let shopList = people[indexPath.row]
cell.items!.text = shopList.valueForKey("name") as? String
let shopList2 = ids[indexPath.row]
cell.itemsId!.text = shopList2.valueForKey("logo") as? String
return cell
}
Try this if problem still exists please add code for numberOfRowsInSection in question
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "ShopList")
//3
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
people = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! itemsList
let shopList = people[indexPath.row]
cell.items!.text = shopList.valueForKey("name") as? String
cell.itemsId!.text = shopList.valueForKey("logo") as? String
return cell
}

Swift: Deleting core data, table won't update

This code seems to be deleting the actual data ok however the table doesn't update without the row unless I force close the app and reopen.
Thanks in advance.
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var lists = [NSManagedObject]()
var deleteListIndexPath: NSIndexPath? = nil
#IBOutlet weak var myTableView: UITableView!
#IBAction func addItem(sender: AnyObject) {
let alert = UIAlertController(title: "New List", message: "Add a new list", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default) { (UIAlertAction) -> Void in
let textField = alert.textFields![0] as UITextField
if textField != "" {
self.saveItem(textField.text!)
self.myTableView.reloadData()
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alert.addTextFieldWithConfigurationHandler(nil)
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return lists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let myCell:UITableViewCell = myTableView.dequeueReusableCellWithIdentifier("Prototype1")!
let list = lists[indexPath.row]
print(lists[indexPath.row])
myCell.textLabel?.text = list.valueForKey("listTitle") as? String
return myCell
}
override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.dataSource = self
self.myTableView.delegate = self
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"List")
// you may need to sort the list
let sortDescriptor1 = NSSortDescriptor(key: "listTitle", ascending: true)
let sortDescriptors = [sortDescriptor1]
fetchRequest.sortDescriptors = sortDescriptors
var fetchedResults : [NSManagedObject] = []
do
{
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
}
catch
{
return
}
for list in fetchedResults
{
lists.append(list)
}
self.myTableView.reloadData()
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete ) {
// Find the LogItem object the user is trying to delete
let logItemToDelete = lists[indexPath.row]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
managedContext.deleteObject(logItemToDelete)
self.myTableView.reloadData()
}
}
func saveItem(Saveitem: String)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: managedContext)
let item = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
item.setValue(Saveitem, forKey: "listTitle")
do {
try managedContext.save()
lists.append(item)
}
catch {
print("Error")
}
}
}
You can use NSFetchedResultsController class with their NSFetchedResultsControllerDelegate protocol methods. And then you can update, delete, insert or move objects very easily!
If your tableView doesn't updated? You can use viewDidAppear function.
I think you still need to remove the "logItem" from the lists object.
lists.removeAtIndex(indexPath.row)
I could give you a more precise answer if you upload the code from this function at the tableview datasource protocol:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

remove string from coredata swift

i am trying to remove one string from coredata and i created this func:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
self.statusArray.removeAtIndex(indexPath.row)
self.tableView.reloadData()
// Rimuovo da CoreData
var appDel = UIApplication.sharedApplication().delegate as! AppDelegate
var context : NSManagedObjectContext! = appDel.managedObjectContext!
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
let request = NSFetchRequest(entityName: "Status")
request.returnsObjectsAsFaults = false
let result:NSArray = context.executeFetchRequest(request, error: nil)!
context.deleteObject(result[indexPath.row] as! Status)
context.save(nil)
}
}
but actually this one give me the SIGABRT error when i try to delete something from the tableView! What is wrong with this function?
Try the following example for deleting.It might be helpful.
var request = NSFetchRequest(entityName: "Status")
var new_sync_details = NSEntityDescription.insertNewObjectForEntityForName("Status", inManagedObjectContext: context) as! NSManagedObject
var err: NSErrorPointer?
if var results = context.executeFetchRequest(request, error: nil) as? [Status] {
println("\nResults count: \(results.count)")
for param in results{
println("\nResults param: \(param)")
new_sync_details.managedObjectContext!.deleteObject(param as NSManagedObject)
}
}

Resources