魔法记录核心数据错误

时间:2016-11-17 12:50:46

标签: ios swift core-data magicalrecord

魔法记录核心数据错误“CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误。 - [__ NSCFSet addObject:]:尝试使用userInfo插入nil (NUL“

func insertChecklistItem(checklistst:Checklist,checklistItemDictionary:NSDictionary, localContext:NSManagedObjectContext) -> Bool
{
    var isInserted = false

    MagicalRecord.save ({
        (context:NSManagedObjectContext) in

        let checklistItem:ChecklistItem = ChecklistItem.mr_createEntity(in: localContext)!
        if  checklistItemDictionary.value(forKey: "id") != nil
        {
            checklistItem.itemId = checklistItemDictionary.value(forKey: "id")! as? String
        }



        if  checklistItemDictionary["description"] != nil
        {
            checklistItem.itemDescription = checklistItemDictionary.value(forKey: "description")! as? String
        }

        if checklistItemDictionary.value(forKey: "name") != nil
        {
            let NameDicitonary = checklistItemDictionary.value(forKey: "name")! as? NSDictionary
            checklistItem.name = NameDicitonary?.value(forKey: "text") as? String
        }

        let sequanceNumber = checklistItemDictionary["sequencenumber"]! as! NSDictionary
        if sequanceNumber["text"] != nil
        {
            let sequanceNumberText = Int(sequanceNumber["text"]! as! String)
            checklistItem.sequencenumber =  sequanceNumberText as NSNumber?
        }

        checklistItem.checklist = checklistst

        if let scaleDictionary = checklistItemDictionary.value(forKey: "scale")
        {
            isInserted = insertScale(checklistItem: checklistItem, scaleDictionary: scaleDictionary as! NSDictionary, localContext: localContext)

        }
        print("------------CRASH \(checklistItemDictionary["checklistitem"])")

        if checklistItemDictionary["checklistitem"] != nil
        {
            if checklistItemDictionary["checklistitem"]! is NSArray
            {
                let checklistItems = checklistItemDictionary["checklistitem"]! as! NSArray
                for item in checklistItems
                {
                    let checklistSubItemDicitonary = item as! NSDictionary
                    insertChecklistSubItem(checklistst: checklistst, checklistItemDictionary: checklistSubItemDicitonary, localContext: localContext, parentId: checklistItem.itemId!)
                }

            }
            else
            {

            }
        }


    }
    )

    let data = ChecklistItem.mr_findAll()
    for d1 in data!
    {
        let d:ChecklistItem = d1 as! ChecklistItem
        print(d.itemId,d.itemDescription,d.name,d.checklist)
    }


    return isInserted
}

一切都在swift 2.3上运作良好。更新到swift 3.0后,出现上述错误。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您不应使用Magical Record的保存区外的上下文。使用提供的context参数。

在核心数据中,不同的上下文不是线程安全的。