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,
    "prio": 0,
    "category": {
        "name": "green",
        "sort": "A"
        "day": 0,
        "aktiv": true,
        "new": 28,
        "erneut": 0
        "day": 0,
        "aktiv": true,
        "new": 28,
        "erneut": 0
        "day": 0,
        "aktiv": true,
        "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

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 {
    guard let data = try? Data(contentsOf: preloadFileURL) else {
    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 {
            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]
   = _name["name"] as? String
           // <...> and so on for all home-attributes <...>

            let cat = NSEntityDescription.insertNewObject(forEntityName: entityCat, into: self.managedContext) as! Category_entity
            let _wt = json as! [String:Any]
   = (_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

            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


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