0

I looked at the following questions and they did not address the issue I have:

The other suggested links were not even close to my issue. My issue is this - I am gathering data from my own domain, using JSON Decode, and then trying to insert the data into Core Data. Is is working 99% of the time but my app crashes occasionally and I get this error message:

2016-01-07 09:49:33.096 AppleLawnApp[564:400038] 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)

2016-01-07 09:49:33.098 AppleLawnApp[564:400038] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'

I have placed it inside of an action block of code for a button:

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext

    let url = NSURL(string: "http://www.mydomain/file.php")

    if url != nil {  

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

            if error == nil {

                let urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) as NSString!

                let data: NSData = urlContent.dataUsingEncoding(NSUTF8StringEncoding)!

                do {

                    let jsonObject = try (NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSArray)!

                    var index1 = 0

                    while index1 < jsonObject.count {

                        let newEntry = NSEntityDescription.insertNewObjectForEntityForName(MyTableData, inManagedObjectContext: context) as NSManagedObject

                        let maindata = (jsonObject[index1] as! NSDictionary)

                        let this1   = maindata["dBase1"] as! String

                        let this2   = maindata["dBase2"] as! String

                        let this3   = maindata["dBase3"] as! String

                        let this4   = maindata["dBase4"] as! String

                        let this5        = maindata["dBase5"] as! String

                        let this6       = maindata["dBase6"] as! String



                        newEntry.setValue(this1, forKey: "CD1")

                        newEntry.setValue(this2, forKey: "CD2")

                        newEntry.setValue(this3, forKey: "CD3")

                        newEntry.setValue(this4, forKey: "CD4")

                        newEntry.setValue(this5, forKey: "CD5")

                        newEntry.setValue(this6, forKey: "CD6")

                        do {

                            try context.save()

                        }

                        catch {

                            print(error)

                        }

                        let data1 = arrayOne(c1: this1 c2: this2, c3: this3, c4: this4, c5: this5, c6: this6)

                        self.myArray.append(data1)

                        index1++

                    }

                }

                catch {

                    print(error)

                }

            }

        })

        task.resume()

    }        

}
Community
  • 1
  • 1

1 Answers1

0

My first guess, based on the error message, is that you may be getting an unexpected null value back in the json where your model entity has a value marked as non-optional. Perhaps try to test the data before updating the NSManagedObject. Here's a couple of similar ways:

if let this1 = maindata["dBase1"] {

    newEntry.setValue(this1, forKey: "CD1")

} else {

    assertionFailure("missing dBase1")
}

OR, something like this:

guard let this2 = maindata["dBase2"]
    , let this3 = maindata["dBase3"]
    , let this4 = maindata["dBase4"] else {

    assertionFailure("missing data")
    return
}

newEntry.setValue(this2, forKey: "CD2")
newEntry.setValue(this3, forKey: "CD3")
newEntry.setValue(this4, forKey: "CD4")
Daniel
  • 624
  • 7
  • 6