0

so far I have created an app that allows me to add a user to core data. However, I am having difficulty figuring out how to delete a user. The btnSave button achieves what it intends, however I am not having any luck attempting to fetch the user and then perform a delete. Please have a look at my included code and let me know if anyone can help. Thanks much in advance.

 import UIKit
 import CoreData

 class ViewController: UIViewController {

@IBOutlet weak var txtUsername: UITextField!
@IBOutlet weak var txtPassword: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

@IBAction func btnLoad(sender: UIButton) {
    //println("Load Button\(txtUsername.text)")
    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext=appDel.managedObjectContext!

    var request=NSFetchRequest(entityName: "Users")
    request.returnsObjectsAsFaults=false

    request.predicate=NSPredicate(format:"username=%@",""+txtUsername.text)

    var results:NSArray=context.executeFetchRequest(request, error:nil)!
    if results.count>0{
        //for res in results{
        //    println(res)
        var res=results[0] as NSManagedObject
        txtUsername.text=res.valueForKey("username")as String
        txtPassword.text=res.valueForKey("password") as String
        //}

    }else{
        println("0 results or potential error")
    }


}


@IBAction func btnSave(sender: UIButton) {


    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext=appDel.managedObjectContext!

    var newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject
    newUser.setValue("\(txtUsername.text)", forKey: "username")
    newUser.setValue("\(txtPassword.text)", forKey: "password")

    context.save(nil)
    println(newUser)
    println("saved")
}

@IBAction func btnDelete(sender: UIButton) {
    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext=appDel.managedObjectContext!
    var delUser = NSEntityDescription.delete("Users")

    context.delete(nil)
}

}

2 Answers 2

2

An NSManagedObject is simply deleted by passing it to the NSManagedObjectContext.deleteObject(...) method. So you first need a reference to the NSManagedObject for your user, then:

context.deleteObject(user)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I tried the above code and it seems to delete users. However, when I run the app again in the simulator and click the load button, the various users seem to be no longer deleted.
Of course, you have to call saveContext after such actions.
HI @MitchKrendell can you Plase help me with The Exact func you had in your Project. coz i am same issue and Not able to solve Thanks Buddy
0
    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext=appDel.managedObjectContext!

    var request=NSFetchRequest(entityName: "Users")
    request.returnsObjectsAsFaults=false

    request.predicate=NSPredicate(format:"username=%@",""+txtUsername.text)

    var results:NSArray=context.executeFetchRequest(request, error:nil)!
    if results.count>0{
        //for res in results{
        //    println(res)
        var res=results[0] as NSManagedObject
        txtUsername.text=res.valueForKey("username")as String
        txtPassword.text=res.valueForKey("password") as String

        context.deleteObject(res)

        //}

    }else{
        println("0 results or potential error")
    }

    println("deleted")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.