MitchKrendell
MitchKrendell

Reputation: 45

XCode Swift - Deleting from core data

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)
}

}

Upvotes: 0

Views: 1406

Answers (2)

MitchKrendell
MitchKrendell

Reputation: 45

    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")

Upvotes: 0

zisoft
zisoft

Reputation: 23078

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)

Upvotes: 2

Related Questions