对核心数据管理对象的更改不会被保存

时间:2015-07-04 23:41:01

标签: swift core-data

我正在尝试保存核心数据管理对象"People"的三个实例,每个实例都有多个值,例如money,name和profit。

目前这不起作用,没有保存数据。

代码:

    //In view controller showing data
    //Viewdidload
     let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false

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

    if results.count > 0{
        var thisUser = results[0] as! People
        labelMoney.text = "\(thisUser.money)"
        labelName.text = "\(thisUser.name)"
        labelProfit.text =  "\(thisUser.profit)"
        if results.count > 1{
            var thisUser = results[1] as! People

        }
        if results.count > 2{
            var thisUser = results[2] as! People

        }

    }

  //In view controller creating name and money
  //in button
  let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false
    //request.predicate = NSPredicate(format: "username = %@", txtUsername.text)

    var results:NSArray = context.executeFetchRequest(request, error: nil)!
    if results.count > 0{
        var yes = results[0] as! NSManagedObject
        yes.setValue(money, forKey: "money")
        yes.setValue(name, forKey: "name")
    }
 // In game view controller saving data
 //In button
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let ent = NSEntityDescription.entityForName("People", inManagedObjectContext: context)

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false

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

    var newUser = results[thisPerson] as! People
    newUser.setValue(profit, forKey: "profit")
    newUser.setValue(money, forKey: "money")

    context.save(nil)

1 个答案:

答案 0 :(得分:0)

    //In view controller showing data
    //Viewdidload

将appDel和您的上下文声明为类变量。在viewDidLoad中分配它们

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

尝试更具体地命名上下文请求,这将使您的代码清晰可辨。当您从多个实体开始提取或使用多种类型的上下文(例如CGContext)时,它会变得混乱

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false

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

结果也可以是nil,result.count会抛出错误。你如何处理结果有点奇怪,但我假设你还没有编写代码来处理结果。

    if results.count > 0{
        var thisUser = results[0] as! People
        labelMoney.text = "\(thisUser.money)"
        labelName.text = "\(thisUser.name)"
        labelProfit.text =  "\(thisUser.profit)"
        if results.count > 1{
            var thisUser = results[1] as! People

        }
        if results.count > 2{
            var thisUser = results[2] as! People

        }

    }

  //In view controller creating name and money
  //in button

如果将它们声明为类变量而不是在本地范围内声明它们,则可以删除它

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

您是否在此处获取已保存的数据并分配更新的值?如果没有,则无需在此处获取。如果是,则应使用if else语句处理它。如果有人,请更新值,否则创建新人......

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false
    //request.predicate = NSPredicate(format: "username = %@", txtUsername.text)

    var results:NSArray = context.executeFetchRequest(request, error: nil)!
    if results.count > 0{
        var yes = results[0] as! NSManagedObject
        yes.setValue(money, forKey: "money")
        yes.setValue(name, forKey: "name")
    }
 // In game view controller saving data
 //In button
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let ent = NSEntityDescription.entityForName("People", inManagedObjectContext: context)

    let request = NSFetchRequest(entityName: "People")
    request.returnsObjectsAsFaults = false

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

    var newUser = results[thisPerson] as! People
    newUser.setValue(profit, forKey: "profit")
    newUser.setValue(money, forKey: "money")

您的命名在这里令人困惑:如果它是新用户,则它不能是获取的结果。获取仅用于保存的数据,因此保存的用户。

    context.save(nil)

创建新用户:

var newUser =  NSEntityDescription.insertNewObjectForEntityForName("People", inManagedObjectContext: context!) as! People
newUser.profit = 42
newUser.money = 1984

context.save()

回顾:

  • 尝试改进您的命名

  • Fetches仅用于更改/显示/使用尚未加载的现有已保存数据。如果您刚刚创建了一个新实例,则无需获取,它仍然存在。

  • 使用insertNewObjectForEntityForName方法创建新实例。分配您想要的值并保存。

  • 另请参阅重置托管对象上下文。或者发布已加载/最近保存的数据。这样可以防止在保存其他内容时更改实例。

  • 另外,请查看viewWillDisappear和willGoToBackground方法,以便在应用程序进入后台或远离视图时自动保存数据。