Jake2Finn Jake2Finn - 25 days ago 6
Swift Question

array items not saved properly in core data entity

I have the problem that only the last item of my string array is saved in the required core data entity.

I have a multi-dimensional array called "itemNames". I flatMap this array to a simple one-dimensional array to iterate through it.
The loop creates for every item a newItem that is saved in core data entity "CALC_VALUES" in the attribute "calc_item".

The console output for newItem looks as aspected.

However, when I load the core data entry for entity "CALC_VALUES", I receive console output 2. It shows me that only the last item, "item9" is saves in the according entity (cf. console output 2).

What do I have to change for my code to save all items when iteration through my array?

Code (swift 3):

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var calc_values = [CALC_VALUES]()
let itemNames = [["item1", "item2", "item3", "item4", "item5"], ["item6", "item7", "item8", "item9"]]

//----------- save items -----------//

// flat multidimensional array to array
let flatItemNames = itemNames.flatMap({ $0 })

// save each item from flatItemNames to core data entity "CALC_VALUES" in attribute "calc_item":
for itemName in flatItemNames {
var newItem = NSEntityDescription.insertNewObject (forEntityName: "CALC_VALUES", into: context) as NSManagedObject
newItem.setValue(itemName, forKey: "calc_item")
(UIApplication.shared.delegate as! AppDelegate).saveContext()

// Checkpoint 1: Check newItem values
print ("***This is newItem: *** \(newItem)")
}


//----------- load items -----------//

// Checkpoint 2: Check core data entry
let fetchRequest: NSFetchRequest<CALC_VALUES> = CALC_VALUES.fetchRequest()
do {
if let results = try context.fetch(fetchRequest) as? [CALC_VALUES] {
calc_values = results as! [CALC_VALUES]
}
} catch {print ("no results")}
print ("***This is calc_values: *** \(calc_values)")


Console output Checkpoint 1:

***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6080000ad4a0> (entity: CALC_VALUES; id: 0x608000232a00 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F3> ; data: {
"calc_item" = "item1";
"calc_value" = 0;
})
***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6000000ac1e0> (entity: CALC_VALUES; id: 0x600000235320 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F4> ; data: {
"calc_item" = "item2";
"calc_value" = 0;
})
***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6000000ab880> (entity: CALC_VALUES; id: 0x600000236c80 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F5> ; data: {
"calc_item" = "item3";
"calc_value" = 0;
})

...


Console output Checkpoint 2:

***This is calc_values: *** [<StartUpKit.CALC_VALUES: 0x6000000aac20> (entity: CALC_VALUES; id: 0xd000000001f00000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p124> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aabc0> (entity: CALC_VALUES; id: 0xd000000001f40000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p125> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aab60> (entity: CALC_VALUES; id: 0xd000000001f80000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p126> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aab00> (entity: CALC_VALUES; id: 0xd000000001fc0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p127> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aaaa0> (entity: CALC_VALUES; id: 0xd000000002000000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p128> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aaa40> (entity: CALC_VALUES; id: 0xd000000002040000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p129> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa9e0> (entity: CALC_VALUES; id: 0xd000000002080000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p130> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa980> (entity: CALC_VALUES; id: 0xd0000000020c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p131> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa680> (entity: CALC_VALUES; id: 0xd000000002100000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p132> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa5c0> (entity: CALC_VALUES; id: 0xd000000002140000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p133> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9ea0> (entity: CALC_VALUES; id: 0xd000000002180000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p134> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9d80> (entity: CALC_VALUES; id: 0xd0000000021c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p135> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9de0> (entity: CALC_VALUES; id: 0xd000000002200000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p136> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9cc0> (entity: CALC_VALUES; id: 0xd000000002240000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p137> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a99c0> (entity: CALC_VALUES; id: 0xd000000002280000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p138> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9900> (entity: CALC_VALUES; id: 0xd0000000022c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p139> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9240> (entity: CALC_VALUES; id: 0xd000000002300000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p140> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9120> (entity: CALC_VALUES; id: 0xd000000002340000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p141> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9180> (entity: CALC_VALUES; id: 0xd000000002380000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p142> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9060> (entity: CALC_VALUES; id: 0xd0000000023c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p143> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8d60> (entity: CALC_VALUES; id: 0xd000000002400000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p144> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8ca0> (entity: CALC_VALUES; id: 0xd000000002440000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p145> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a6240> (entity: CALC_VALUES; id: 0xd000000002480000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p146> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a84c0> (entity: CALC_VALUES; id: 0xd0000000024c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p147> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac3c0> (entity: CALC_VALUES; id: 0xd000000002500000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p148> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac420> (entity: CALC_VALUES; id: 0xd000000002540000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p149> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8040> (entity: CALC_VALUES; id: 0xd000000002580000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p150> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac480> (entity: CALC_VALUES; id: 0xd0000000025c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p151> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ab040> (entity: CALC_VALUES; id: 0xd000000002600000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p152> ; data: {
"calc_item" = "item9";
"calc_value" = 0;
})]

Answer

You have to keep (UIApplication.shared.delegate as! AppDelegate).saveContext() outside of for loop.

below is the complete code.

    let itemNames = [["item1", "item2", "item3", "item4", "item5"], ["item6", "item7", "item8", "item9"]]
    let flatItemNames = itemNames.flatMap({ $0 })

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    for itemName in flatItemNames {
        let managedContext = NSEntityDescription.insertNewObject(forEntityName: "CALC_VALUES", into: context)
        managedContext.setValue(itemName, forKey: "calc_item")
    }
    do {
        try context.save()
    } catch let error as NSError  {
        print("Error While Saving Data: \(error.userInfo)")
    }

To view your db chanages you can use SQLite Manager

Below is the screenshot.

enter image description here