【问题标题】:Magical Record Core data Error魔法记录核心数据错误
【发布时间】:2017-04-01 00:35:43
【问题描述】:

Magical Record Core data error "CoreData: error: Serious application error. 在 Core Data 更改处理期间捕获到异常。这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的错误。-[__NSCFSet addObject:]: 尝试使用 userInfo 插入 nil (无"

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 后,出现上述错误。提前致谢。

【问题讨论】:

    标签: ios swift core-data magicalrecord


    【解决方案1】:

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

    在 Core Data 中,不同的上下文不是线程安全的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2012-01-20
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多