删除多对多关系coredata

时间:2016-12-21 12:32:53

标签: ios swift core-data

我有很多关系," BillSplitters"到"项目",即时尝试。从billplitters的tableview中删除一个billlotitter,但是当我查看我的项目时,他们仍然列出与它们相关但尚未删除的billplitters。我已经尝试将删除规则从nullify更改为级联项目和帐单拆分器,但似乎没有任何变化。

这是我认为相关的代码:

BillSplitter表视图:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        let billSplitter = allBillSplitters[indexPath.row]
        let currentSplitters = self.bill.mutableSetValueForKey("billSplitters")
        let managedContext = self.bill.managedObjectContext

        removeBillSplitter(billSplitter)
        currentSplitters.removeObject(billSplitter)


        do {
            try managedContext!.save()
        }
        catch let error as NSError {
            print("Core Data save failed: \(error)")
        }
        print(currentSplitters)
        tableView.reloadData()
    }
}

func removeBillSplitter(billSplitter: BillSplitter) {
    if let index = allBillSplitters.indexOf(billSplitter) {
        allBillSplitters.removeAtIndex(index)
    }
}

项目表视图:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    fetchBillItems()

    let cell: NewBillSplitterItemCell = tableView.dequeueReusableCellWithIdentifier("NewBillSplitterItemCell") as! NewBillSplitterItemCell
    let item = allItems[indexPath.row]
    let numberOfSplitters = item.billSplitters?.count

    if numberOfSplitters == 0 {
        cell.currentSplitters.text = "No one is paying for this item yet."
    } else {

        var splitterList = "Split this item with: "
        let itemSplitters = item.billSplitters?.allObjects as! [BillSplitter]
        for i in 0...Int((numberOfSplitters)!-1) {
            splitterList += "\(itemSplitters[i].name!), "
        }
        cell.currentSplitters.text = splitterList
    }

    cell.name.text = item.name
    cell.price.text = "£\(item.price!)"

    return cell
}

我们的想法是从项目表视图中选择项目以分配给一个billlitter,它可以正常工作然后重定向到billplitter表视图,当从这个表视图中删除一个billlotitter它工作正常并且不再列出,但是当回到项目表视图,它仍然列出了billSplitters迭代itemSplitters for循环,我不明白为什么?我是否必须找到与账单拆分器关联的每个项目并从那里删除它们?我想那是级联会做什么的?

0 个答案:

没有答案