在我的表视图中使用NSFetchedResultsController时,在创建新的NSManagedObject后立即正确显示数据,但更新后将删除所有行/部分。下面,当调用update()方法并保存联系人时,我的print语句输出正在从表视图中删除行和部分。调用create()方法时,正在插入行/部分(如预期的那样)。
以下是运行后的两组不同输出:
从API请求中检索到信息后,如果项目已经存在(由唯一ID指定),我将更新相应的核心数据模型,如下所示:
func update(oldContact: NSManagedObject) -> Bool {
//updates contact
let contact = populateObject(oldContact)
// Delete existing phones
if let phoneDataSet = contact.valueForKeyPath("phones") {
let phonesArray = phoneDataSet.allObjects as! [NSManagedObject]
for object in phonesArray {
context.deleteObject(object)
}
}
// Add phones from response
for phone in phones {
phone.createForContact(contact)
}
// Save contact
do {
try contact.managedObjectContext?.save()
print("saving contact")
return true
} catch {
let nserror = error as NSError
print("error upong saving")
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
return false
}
func populateObject(object: NSManagedObject) -> NSManagedObject {
object.setValue(self.name, forKey: "name")
object.setValue(self.id, forKey: "id")
object.setValue(self.firstLetter, forKey: "firstLetter")
return object
}
如果该项目在核心数据中已不存在,则会按如下方式创建:
func create() -> Bool {
// Quit if contact already exists.
let data = CoreData().searchObject("Contact", predicate: "id", value: String(self.id))
guard data == nil else { fatalError("Attempted to insert duplicate contact") }
//creates a new contact NSManagedObject
var newContact = NSEntityDescription.insertNewObjectForEntityForName("Contact", inManagedObjectContext: context)
//sets the contact values
newContact = populateObject(newContact)
//creates Phone NSManagedObject, then makes a relationship with contact
for phone in self.phones {
phone.createForContact(newContact)
}
do {
//saves the contact object; also saves the relationship with the phone number
try newContact.managedObjectContext?.save()
print("Creating contact")
return true;
} catch {
let nserror = error as NSError
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
return false
}
我的FetchedResultsController委托方法如下所示:
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Update:
if let indexPath = indexPath {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? PeopleAndPlaceCell {
configureCell(cell, atIndexPath: indexPath)
tableView.reloadRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
print("UPDATING ROW")
}
}
break;
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
print("INSERTING ROW")
}
break;
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
print("DELETING ROW")
}
break;
case .Move:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if let newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
break;
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Update:
print("UPDATE SECTION")
break
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
print("INSERTING SECTION")
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
print("DELETING SECTION")
case .Move:
break
}
}
更新:
联系人与电话的关系是一对多,从电话到联系人是一对一的关系。 Phone实体中联系人关系的删除规则是Cascade。它也是联系人实体中电话关系的级联。
答案 0 :(得分:1)
问题是contact
实体的Phone
关系的“级联”删除规则的结果。在update()
方法中,删除给定Phone
的所有“旧”Contact
个对象。级联删除规则也会导致Contact
被删除。将此删除规则更改为“Nullify”。 (您可以将phones
实体中的反向关系Contact
保留为“级联”:当您删除Contact
时,它将删除所有关联的Phones
)