2016-11-17 135 views
0

魔法记录核心数据错误“CoreData:错误:严重的应用程序错误,在核心数据更改处理过程中发生异常,这通常是NSManagedObjectContextObjectsDidChangeNotification观察者中的一个错误 - [__ NSCFSet addObject:]:尝试插入比零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 
} 

一切行之有效的SWIFT 2.3。更新到SWIFT 3.0后,上面的错误提前来了。谢谢你。

回答

0

你不应该从使用环境之外神奇纪录的保存块。使用提供的context参数。

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