将JSON保存到核心数据

时间:2016-01-07 19:25:54

标签: php json swift core-data

我查看了以下问题但他们没有解决我的问题:

其他建议的链接甚至不是我的问题。我的问题是这个 - 我正在使用JSON Decode从我自己的域收集数据,然后尝试将数据插入到Core Data中。是99%的时间工作,但我的应用程序偶尔崩溃,我收到此错误消息:

  

2016-01-07 09:49:33.096 AppleLawnApp [564:400038] CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误。 - [__ NSCFSet addObject:]:尝试使用userInfo(null)插入nil

     

2016-01-07 09:49:33.098 AppleLawnApp [564:400038] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFSet addObject:]:尝试插入nil'

我把它放在按钮代码的动作块中:

    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()

    }        

}

1 个答案:

答案 0 :(得分:0)

我的第一个猜测,基于错误消息,您可能会在json中获得意外的空值,其中您的模型实体具有标记为非可选的值。也许在更新NSManagedObject之前尝试测试数据。以下是几种类似的方式:

if let this1 = maindata["dBase1"] {

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

} else {

    assertionFailure("missing dBase1")
}

OR,像这样:

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")