Martheli Martheli -4 years ago 71
iOS Question

Insert two rows of data into Core Data Swift 3

I have a core data stack set up with an entity called Device with two attributes, asset_tag and location. I have two arrays that I have set up like this:

var assetTag = ["53","35","26","42","12"]
var location = ["SC", "FL", "NA", "NY", "CF"]


The first part of my code loops through the first array and adds each number into the asset_tag attribute like this:

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

for device in assetTag {

let newArray = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context)

newArray.setValue(device, forKey: "asset_tag")
}


This adds each value to an array so I can print them out later and this works perfectly. I would like to do the same thing with the second array and add them to my second attribute but when I try, it doesn't add the data correctly. This is what I have:

for locations in location {

let secondArray = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context)

secondArray.setValue(locations, forKey: "location")

}


When I print out the results, I get a bunch of nill values:

[<Device: 0x600000281130> (entity: Device; id: 0xd000000001580000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p86> ; data: {
"asset_tag" = nil;
devices = nil;
location = CF;
}), <Device: 0x60000009f040> (entity: Device; id: 0xd0000000015c0000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p87> ; data: {
"asset_tag" = 53;
devices = nil;
location = nil;
}), <Device: 0x6000002810e0> (entity: Device; id: 0xd000000001600000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p88> ; data: {
"asset_tag" = nil;
devices = nil;
location = NY;


I am not sure where those nils are coming from.

EDIT:

That looks better but I am getting another attribute called Devices with nils:

"asset_tag" = 12;
devices = nil;
location = CF;


Also when I print out the results from core data like above, the order of the values is not the same as the order that I defined my array.

"asset_tag" = 12;
devices = nil;
location = CF;

"asset_tag" = 53;
devices = nil;
location = SC;


The results above show 12 and then 53 and CF and then SC but thats a different order than the original arrays that were set up.

Answer Source

You have to create 5 instances with 2 attributes. Your code is going to create 10 different instances. A Core Data entity is like a class with multiple properties.

The solution is to use an index based loop and assign both attributes.

For a better understanding name the Core Data instance device rather than newArray. The following code uses an array to keep the instances.

The assert line is to check if both arrays contain the same number of items.

var devices = [Device]()

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

assert(assetTag.count == location.count, "Both arrays must have the same number of items")
for i in 0..<assetTag.count {

    let device = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context) as! Device

    device.asset_tag = assetTag[i]
    device.location = location[i]
    devices.append(device)
}

Side note:

Declare string attributes which are supposed to have a value as non-optional in the Core Data model.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download