如何在Swift中覆盖Core Data属性

时间:2014-12-22 18:08:29

标签: ios xcode core-data swift

我尝试更新Core Data中的属性然后显示它。但是,当我告诉程序显示它时,它会显示原始值。

根据以下代码,username设置为String。我知道100%将username保存到Core Data中。但是,当我尝试更新它时,它会保存到Core Data,但会打印出旧值。我怎样才能打印出最新的"值?

import UIKit
import CoreData

class NameSettings: UIViewController {

@IBOutlet var nameText: UITextField!

var userID: String!
var username: String!

override func viewDidLoad() {
    super.viewDidLoad()

    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)

    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var request = NSFetchRequest(entityName: "Users")

    request.returnsObjectsAsFaults = false

    var results: NSArray = context.executeFetchRequest(request, error: nil)!

    var res = results [0] as NSManagedObject

    userID = res.valueForKey("userID") as String
    username = res.valueForKey("username") as String

    println(username)

    nameText.text = username

}

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

@IBAction func saveTapped(sender: AnyObject) {

    //println(userID)

    var query = PFQuery(className:"_User")
    query.getObjectInBackgroundWithId(userID) {
        (update1: PFObject!, error: NSError!) -> Void in
        if error == nil {

            update1["username"] = self.nameText.text
            update1.saveEventually()

            var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)

            var context:NSManagedObjectContext = appDel.managedObjectContext!

            var updateUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject

            updateUser.setValue(self.nameText.text, forKey: "username")

            context.save(nil)

            //println(self.nameText.text)

            self.navigationController?.popToRootViewControllerAnimated(true)


        }
    }

}

}

注意:所有println仅用于调试。

1 个答案:

答案 0 :(得分:3)

正如您的代码现在所示,您不会覆盖任何NSManagedObject;你正在插入一个新的,insertNewObjectForEntityForName

相反,您可以做的是将要保存的NSManagedObject和要保存的NSManagedObjectContext声明为全局变量;然后只需为NSManagedObject相关密钥设置新值,然后再保存到NSManagedObjectContext,例如:

var userID: String!
var username: String!
var res : NSManagedObject!
var context : NSManagedObjectContext!

override func viewDidLoad() {
    super.viewDidLoad()

    //...

    context = appDel.managedObjectContext! // <-- Use global var

    var request = NSFetchRequest(entityName: "Users")

    request.returnsObjectsAsFaults = false

    var results: NSArray = context.executeFetchRequest(request, error: nil)!

    res = results [0] as NSManagedObject // <-- Use global var

    //...
}

@IBAction func saveTapped(sender: AnyObject) {

    //println(userID)

    var query = PFQuery(className:"_User")
    query.getObjectInBackgroundWithId(userID) {
        (update1: PFObject!, error: NSError!) -> Void in

        if error == nil {

            update1["username"] = self.nameText.text
            update1.saveEventually()

            res.setValue(self.nameText.text, forKey: "username")

            context.save(nil)

            //println(self.nameText.text)

            self.navigationController?.popToRootViewControllerAnimated(true)

        }
    }
}