Core Data - preloading JSON - relationship (to-many)

Hi at all,

I need some help for my preloader. I’ve a db.json like this:

{
"home": [{
    "name":"some Namer",
    "info":"lorem ipsum",
    "active": true,
    "dateB":"2014/09/27",
    "dateH":"2014/09/27",
    "prio": 0,
    "category": {
        "name": "green",
        "sort": "A"
    },
    "task":[{
        "name":"Task1",
        "info":"someInfo",
        "day": 0,
        "aktiv": true,
        "okt":"2014/09/27",
        "new": 28,
        "erneut": 0
         },{
        "name":"Task2",
        "info":"someInfo2",
        "day": 0,
        "aktiv": true,
        "okt":"2014/09/27",
        "new": 28,
        "erneut": 0
         },{
      "name":"Task3",
        "info":"someInfo3",
        "day": 0,
        "aktiv": true,
        "okt":"2014/09/27",
        "new": 28,
        "erneut": 0
    }]},{(file starts again with new "item" of home.... etc)

My preload function( incl. relationthip inside core data) works for:

  • HOME Item
  • CATEGORY iTEM

But if I try to preload the TASK item the preload/import crashed

The relationship from Home to Task is a one-to-many like the other to Category but on the other direction.

home.category_relationthip - Category (to one in home)
home.task_relationship - Task (to many in home)

Here is my code:

fileprivate func preloadWithRelationships() {
    guard let preloadFileURL = Bundle.main.url(forResource: "preload_DB", withExtension: "json") else {
        return
    }
    
    guard let data = try? Data(contentsOf: preloadFileURL) else {
        return
    }
    
    do {
let jsonResult = try? JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let jsonArray = jsonResult?.value(forKey: "home") as! NSArray

let entityCat: String = String(describing: Category_entity.self)
let entityTask: String = String(describing: Task_entity.self)


 for json in jsonArray {
            
            //Homeinfos
            let entityHome: String = String(describing: Home_entity.self)
            let home = NSEntityDescription.insertNewObject(forEntityName: entityHome, into: self.managedContext) as! Home_entity
            
            let _name = json as! [String:Any]
            home.name = _name["name"] as? String
           // <...> and so on for all home-attributes <...>



            //Category
            let cat = NSEntityDescription.insertNewObject(forEntityName: entityCat, into: self.managedContext) as! Category_entity
            let _wt = json as! [String:Any]
            cat.name = (_wt["category"] as! NSDictionary)["name"] as! String?
            home.category_relationship = cat
            
            let _sort = json as! [String:Any]
            cat.sortNumber = (_sort["category"] as! NSDictionary)["sort"] as! String?
            home.category_relationship = cat



            //Task
            let task = NSEntityDescription.insertNewObject(forEntityName: entityTask, into: self.managedContext) as! Task_entity

            let taskname = json as! [String:Any]
            let name = (taskname["task"] as! NSDictionary)["name"] as! String?   ////<-----! Crash ERROR Array into Dictonary

            room.addToExtraListe_rls(extra)

           // <...> and so on for all task-attributes BUT at this time it crashes at the first line...<...>
}
        
        saveContext()
        
    } catch let error as NSError {
        print("Fehler: \(error.localizedDescription)")
    }
}