2017-06-16 26 views
0

我在我的應用程序中使用coreData保存多個表(幾乎在30個左右),它工作正常,但隨機崩潰,出現此異常:在覈心數據更改處理期間發生異常,嘗試向用戶信息(null)插入nil

[error] 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 (null) 
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 (null) 
2017-06-16 15:05:56.043490+0500 Sales CRM[619:85792] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil' 
*** First throw call stack: 
(0x1872c6fe0 0x185d28538 0x1872c6f28 0x1871e316c 0x1895c2028 0x1895c1218 0x1895c97d0 0x1895c019c 0x1001d6888 0x1001bb78c 0x1000a43d8 0x1878d034c 0x1878e8048 0x187d95814 0x187cda770 0x187ccab28 0x187d97bb0 0x100f85a10 0x100f932e8 0x100f89634 0x100f95630 0x100f9539c 0x186387100 0x186386cac) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我尋覓了很多,但大部分的解決方案都是在Objective C,甚至我跟着一些解決方案,但它都沒有工作。

這裏是我的代碼:

public class ServiceCalls : NSManagedObject { 
    /* 
    class func getContext() -> NSManagedObjectContext { 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 

     let moc = NSManagedObjectContext(concurrencyType:.mainQueueConcurrencyType) 
     let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) 
     privateMOC.parent = moc 

     privateMOC.perform({ 
      do { 
       try privateMOC.save() 
      } catch { 
       fatalError("Failure to save context: \(error)") 
      } 
     }) 
     return appDelegate.persistentContainer.viewContext 
    } 
    */ // tried this but didn't work as well 

    class func getContext() -> NSManagedObjectContext { 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     return appDelegate.persistentContainer.viewContext 
    } 

以上就是我的背景被稱爲每次我保存或從任何數據庫獲取任何東西。以下是我如何保存coreData:

let context = getContext() 
    let entity = NSEntityDescription.entity(forEntityName: "DoctorsDetail_Tbl", in: context) 
    let newDoc = NSManagedObject(entity: entity!, insertInto: context) 

    newDoc.setValue(empid, forKey: "empId") 
    newDoc.setValue(doctorid, forKey: "docId") 



    //save the object 
    do { 
     try context.save() 
     print("saved Doctors in Database yayy!") 

    } catch let error as NSError { 
     print("Could not save \(error), \(error.userInfo)") 
    } catch { 
     let nserror = error as NSError 
     fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
    } 
} 
+0

標記和正確的,如果你找到解決方案..更好的指導給其他用戶。 – vaibhav

+0

@vaibhav解決方案沒有工作,仍然有同樣的問題! –

回答

0

有時你得到你的數據源(empid, doctorid)nilcoredata

檢查nil插入要插入值到數據庫之前,你的想。

func test() { 
    for (key, value) in self { 
     if let unwrappedValue = value.asOptional { 
      print("Unwrapped value for '\(key)' is '\(unwrappedValue)'") 
     } else { 
      print("Value for '\(key)' is nil") 
     } 
    } 
} 

用途:

var dict: [String: AnyObject?] = [ 
    "key1" : "value1", 
    "key2": nil,    // your program is unexpectedly crashing here 
    "key3": "somevalue" 
] 

dict.test() // calling test fuction 

濾波器零陣列:

extension Array { 
    static func filterNils(array: [T?]) -> [T] { 
     return array.filter { $0 != nil }.map { $0! } 
    } 
} 

用途:

var array:[Int?] = [1, nil, 2, 3, nil] 

Array.filterNils(array) 

希望這會有所幫助。

+0

我明白了,但大部分時間它的工作狀態都很好,它只是偶爾隨機崩潰。 –

+0

@shahtajkhalid,得到你的觀點,所以我建議你在插入之前每次檢查數組或字典中的nil值。 – vaibhav

+0

@shahtajkhalid,檢查我更新的答案。 – vaibhav

相關問題