Uploading records to CoreData is quite simple. This function relies on the CoreDataConduit class in my last post for retrieving and saving the managed object context.

The dictionary of data you wish to insert is the first parameter - within the funciton you can then determine how this data is mapped to your Core Data object.

The second parameter of this function is the Entity name that you are inserting the record into.

Pre-requisites to this process is setting up your Data Model - if you are unsure of how to do this, there is a guide here.

class func updateObjectWithInfo(theInfo:Dictionary<String, String>, forEntity theEntityName:String){

        if let myManagedObjectContext = CoreDataConduit.returnMyManagedObjectContext() {

            let theEntityDescription = NSEntityDescription.entityForName("YourEntityName", inManagedObjectContext: myManagedObjectContext)

            theObject = EntityName(entity: theEntityDescription, insertIntoManagedObjectContext: myManagedObjectContext)

            //DUPLICATE THE BELOW CHUNCK OF CODE FOR EACH ITEM YOU WANT TO ADD TO COREDATA

            if let theAttribute = theInfo.["attributeName"] as? String {

               theObject?.attributeName = theAttribute
            }
            CoreDataConduit.saveContext()
        }
    }

You can combine this with my last post about scanning a CSV with Swift. Please observe...

 CSVScanner.runFunctionOnRowsFromFile(["title", "body", "category"], withFileName: "fileName.csv", withFunction: {

        (aRow:Dictionary<String, String>) in

         updateObjectWithInfo(aRow , forEntity:"YourEntityName")

})

This will insert the data from the csv into CoreData as it's being read. Alternatively you can also wait until the CSV is fully scanned and you have an array with all of the data (this is what I did in my last post about scanning a CSV). You then just need to loop through the array and use the updateObjectWithInfo function on each item.

Happy coding!

Jeremy