CoreData 更新后不保存信息
Posted
技术标签:
【中文标题】CoreData 更新后不保存信息【英文标题】:CoreData not saving information after being updated 【发布时间】:2015-07-29 15:47:13 【问题描述】:这个函数应该将当前日期添加到一个列表中,只要按下 addButton 就存储在内存中,但是当应用程序关闭并重新加载时,新日期不会出现。切换视图后数据似乎也消失了。
var selectedPersonFirst = ""
var selectedPersonLast = ""
let today = NSDate()
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBAction func addButtonPressed(sender: AnyObject)
let entityDescription = NSEntityDescription.entityForName("People", inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entityDescription
let firstPred = NSPredicate(format: "(firstName = %@)", selectedPersonFirst)
let secondPred = NSPredicate(format: "(lastName = %@)", selectedPersonLast)
let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [firstPred, secondPred])
request.predicate = predicate
var error: NSError?
var objects = managedObjectContext?.executeFetchRequest(request, error: &error)
if let results = objects
let days = results[0].valueForKey("days") as! NSMutableArray
days.addObject(NSDate())
let person = results[0] as! People
person.setValue(days, forKey: "days")
managedObjectContext!.save(nil)
self.tableView.reloadData()
这是 NSManagedObject 的 people 子类:
import Foundation
import CoreData
class People: NSManagedObject
@NSManaged var days: AnyObject
@NSManaged var dayZero: NSDate
@NSManaged var firstName: String
@NSManaged var gender: String
@NSManaged var lastName: String
在其中创建days
NSMutableArray 的 ViewController:
import UIKit
import CoreData
class NewPersonViewController: UIViewController
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var gender: UISegmentedControl!
@IBOutlet weak var dayZeroDate: UIDatePicker!
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var personName : String = ""
var currentDayNumber : Int? = nil
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "DoneNewPersonSegue"
let entityDescription = NSEntityDescription.entityForName("People", inManagedObjectContext: managedObjectContext!)
let person = People(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
person.firstName = self.firstName.text
person.lastName = self.lastName.text
person.days = NSMutableArray()
person.gender = gender.description
person.dayZero = dayZeroDate.date
save()
func save()
var error : NSError?
if(managedObjectContext!.save(&error) )
println(error?.localizedDescription)
数据模型:
【问题讨论】:
如何在您的数据模型中声明days
?您在哪里创建用于它的初始 NSMutableArray
?
@TomHarrington days
在实体People
中声明为Transformable
类型的属性。最初的 NSMutableArray 是另一个视图类,我将更新问题以包含代码
你确定 objects 不是 nil(即 fetch 没有抛出错误)吗?另外,如果它不是 nil,它可能包含几个对象?我注意到你的谓词使用.OrPredicateType
,我想你可能想要.AndPredicateType
?
@pbasdf 感谢您发现谓词问题,但重新加载应用后对象仍然没有出现。另外,我检查了对象在保存之前不是 nil
objects
我指的是 fetch 返回的数组。如果由于某种原因 fetch 出现错误,objects
将为 nil,并且您的 if let results = objects
中的所有内容都将被跳过。
【参考方案1】:
问题是我使用的是可变类型,所以更改没有调用 setter 方法。我更改了代码以创建对象的副本,以便识别更改:
let days1 = results[0].valueForKey("days") as! NSMutableArray
let days = days1.mutableCopy() as! NSMutableArray
days.addObject(NSDate())
let person = results[0] as! People
person.setValue(days, forKey: "days”)
【讨论】:
以上是关于CoreData 更新后不保存信息的主要内容,如果未能解决你的问题,请参考以下文章
使用 WebService 更新 TableView 并保存到 CoreData
tableview.reloaddata() 在使用 coredata 时搜索栏取消按钮后不起作用