0
votes

Magical Record Core data error "CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (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
}

Everything worked well on swift 2.3. After updating to swift 3.0, above error came. Thanks in advance.

1

1 Answers

0
votes

You should not use a context from outside Magical Record's save block. Use the provided context parameter.

In Core Data, different contexts are not thread safe.