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

NSFetchedResultsController is deleting rows instead of updating them after Core Data is being updated

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

这里是运行之后两组不同的输出:

从 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的关系是一对多,从Phone到联系人的关系是一对一。 Phone实体中联系关系的删除规则为级联。它也是 Contact 实体中 phone 关系的级联。

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