iOS Swift - How to store array with Core Data?

16,222

Solution 1

Warning: opinionated answer ahead.

You don't.

Storing things in an array does not make anything easier for you. On the contrary, it will make things much harder just an hour in. Imagine you want to show all Recipes that contain a selected Ingredient. That wouldn't be easy with your array hack, with a proper model it's only a couple line of code.

I would recommend to use a good old relationship with a "Join-entity".

basic Recipe data model

Yes, this is more complicated than hacking something together that barely works. But it's the correct way.

Solution 2

What you was thinking of is exactly what you should do. Core Data is made to store values in array like structure. You should create entity Ingredients and connect your Food entity (or whatever you would like to call it) with relationship with Ingredients entity.

Solution 3

there is a way. You can do each element manually e.g. You have your array:

let employee: NSMutableArray = []

employee.addObject(["name":"Bill","LastName":"Hanks"])
employee.addObject(["name":"Rolex","LastName":"Swarzer"])
employee.addObject(["name":"Clive","LastName":"Martin"])
employee.addObject(["name":"Jimi","LastName":"Hendrix"])

Assuming you have created your coreData with Entity "Employee" and Attributes "name" and "lastname" you do the following to add it in...

let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDel.managedObjectContext

for item in employee {
    do {
        let newUser = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: context)
        newUser.setValue(item["name"], forKey: "name")
        newUser.setValue(item["LastName"], forKey: "lastname")
        try context.save()
    } catch {
        //do nothing
    }

You can then fetch all elements using your fetch request or the NSFetched Results Controller

Share:
16,222
Seong Lee
Author by

Seong Lee

I am a user interface designer based in New Zealand.

Updated on June 22, 2022

Comments

  • Seong Lee
    Seong Lee almost 2 years

    I'm new to iOS development and was wanting to know which data type I should specify to store multiple strings (array). The app is to do with food and I need to store multiple ingredients as one attribute.

    I was thinking of making ingredient as entity, but I just want to make it easy for a starter. I have read about transformable type but people don't seem to recommend using it to store arrays.

  • Seong Lee
    Seong Lee about 9 years
    Thanks :) I will go for the proper way.
  • Seong Lee
    Seong Lee about 9 years
    Thanks for your suggestion. You're right. I should do it properly.
  • Seong Lee
    Seong Lee about 9 years
    Can you please tell me if I need to explicitly create attributes of recipe id and ingredient id in the joint-entity? or does xcode automatically handles it if a relationship is established?
  • Japes
    Japes over 8 years
    I see what you mean about sorting and displaying search results. However, doesnt he now need an array of custom ingrediet objects? My app needs a string array (and wouldnt benefit from a model like the above), and I still dont know how to store the array. any help?
  • Japes
    Japes over 8 years
    it has a list of file paths associated with my main entity, with a max size of 10. So even if I create a new filepath entity, i still need to store an array of filepaths...
  • MartianMartian
    MartianMartian over 6 years
    but where to store these data?
  • MartianMartian
    MartianMartian over 6 years
    but where to store these data?
  • Sreekanth G
    Sreekanth G almost 4 years
    @SaikumarReddy If you directly update a record you will get memory leaks So to avoid memory leaks It's better to make a SingleTon class and update from SingleTon class itself