gpt4 book ai didi

ios - NSFetchedResultsController 正在删除行而不是在更新核心数据后更新它们

转载 作者:可可西里 更新时间:2023-11-01 02:17:22 26 4
gpt4 key购买 nike

在我的 TableView 中使用 NSFetchedResultsController 时,数据在创建新的 NSManagedObject 后立即正确显示,但所有行/部分在更新后都被删除。下面,当调用 update() 方法并保存联系人时,我的打印语句输出行和部分正在从 TableView 中删除。调用 create() 方法时,将插入行/节(如预期的那样)。

运行后两组不同的输出:

enter image description here enter image description here

从 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 委托(delegate)方法如下所示:

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

更新:

Contact 到 Phone 是一对多的关系,而从 Phone 到 Contact 是一对一的关系。 Phone 实体中联系关系的删除规则是级联。对于Contact实体中的phone关系也是级联的。

最佳答案

问题是 Phone 实体的 contact 关系的“级联”删除规则的结果。在您的 update() 方法中,您删除了给定 Contact 的所有“旧”Phone 对象。级联删除规则导致 Contact 也被删除。将此删除规则更改为“无效”。 (你可以在Contact实体中保留反向关系,phones,作为“Cascade”:当你删除一个Contact时,它会删除所有关联的 Phone)。

关于ios - NSFetchedResultsController 正在删除行而不是在更新核心数据后更新它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35975504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com