Core data is Apples almost proprietary system for persistent data storage. It was INSANELY complicated to setup in iOS versions before iOS 10.

Luckily for us though, it is now relatively simple. There are a few things you need to know first but this should be one of the easier tutorials you’ll come across.

How to Use Core Data in iOS 10

Step 1: Use Core Data

Sounds obvious but you need to check ‘Use core data‘ when you first create your project. If you don’t do this then you can add core data later on (but you’ll need to Google the steps).

 

Step 2: Get Context

We need this to get the managed object context that allows us to use core data. In previous iOS versions this was accessed via appDelegate. It still is, but slightly differently in iOS 10 and beyond. Create a getContext() function.

Once that context is returned we move on to…

 

Step 3: Create a Model

I was pretty confused when I first used core data! What? No class or structure to create for my objects?? Well no – Basically you define your data model in the Xcdatamodeld file that was created with your project. Create a new ‘Entity’ in the xcdatamodeld file. Add ‘attributes’ as needed. I’m using strings in the example below but you can have Ints / Booleans etc.

Core data model in Xcode

 

Step 4: Save the Data

Create a function to save the data:

 

Step 5: Retrieve the Data

Create a function to retrieve the data:

 

Step 6 (optional): Fix the ‘Undeclared type Transcription’ error

Highlight the data model, click ‘Editor’ in Xcode and select ‘Create NSManagedObject Subclass’. That should take care of the problem for you. If it doesn’t then read the comments below from Petr and David Brownstone.

And that’s how you use core data in iOS 10 and Swift 3 🙂

Where to go Next

This tutorial comes straight out of The Complete iOS Developer Course. Over 40 hours long it contains everything you need to create awe inspiring apps fast!

Click here to check it out!

How to Use Core Data in iOS 10 (Swift 3)

19 thoughts on “How to Use Core Data in iOS 10 (Swift 3)

  • July 28, 2016 at 9:21 pm
    Permalink

    Do you have the source code available?

    Reply
    • September 11, 2016 at 2:30 pm
      Permalink

      It’s all in the post!

      Reply
  • October 2, 2016 at 9:04 am
    Permalink

    I have an error with the fetch request. It says I am using an undeclared type User. Am i missing something. Adding data works perfectly.

    Reply
  • October 3, 2016 at 11:47 pm
    Permalink

    Hey man, is`n necessary CreateNSManagedObjectSubclass? And your Transcription class? Thanks

    Reply
  • October 8, 2016 at 6:01 pm
    Permalink

    I get an error for the line let fetchRequest: NSFetchRequest = Transcription.fetchRequest() that says: /Users/user/Desktop/CoreDataiOS10Test/CoreDataiOS10Test/AppDelegate.swift:56:42: Use of undeclared type 'Transcription'

    I double checked the spelling of my entity, and I haven’t been able to find a solution to this online that works…

    Reply
  • October 16, 2016 at 9:37 pm
    Permalink

    This is a really great tutorial. Very simpel and is not trying too much at once. Just basics. Very simpel layout.
    How can anyone ask for source code ??

    Reply
  • October 18, 2016 at 9:13 am
    Permalink

    let fetchRequest: NSFetchRequest = Transcription.fetchRequest()

    Thanks for Tutorial. Could you please explain above line? what is Transcription here. How you able to access it?

    Reply
  • October 23, 2016 at 3:40 pm
    Permalink

    Thanks. It helped a lot.

    Reply
  • November 1, 2016 at 9:04 pm
    Permalink

    Worked for me. Thanks!

    Reply
  • November 2, 2016 at 9:42 am
    Permalink

    Thanks again. Also it appears no need to cast to NSManagedObject. This code seems to work for me:

    for trans in searchResults {
    print(trans.audioFileUrlString)
    }

    Just need to make sure @objc(MySubClass) is removed from your NSManagedObject subclass.

    More info here: https://developer.apple.com/videos/play/wwdc2016/242/

    Reply
    • February 9, 2017 at 3:34 pm
      Permalink

      Thanks Petr, I’ve referenced this comment in the article – could be helpful for others!

      Reply
  • November 7, 2016 at 9:56 am
    Permalink

    One point to make clear – when creating entities in xcdatamodeld, be careful to note that in the Data Model Ininspector, the default Codegen is Class Definition i.e. you do not need to create an NSManagedObject SubClass – it is created for you – not visibly but you can see it by command-selecting the entity name in your code. Alternatively, you can change the Codeine entry to create your own subclass. If you leave Codegen as default and you create the subclass, you will get invalid redeclaration messages!

    Reply
    • November 18, 2016 at 2:16 pm
      Permalink

      Good point David, and something I forgot to mention!

      Reply
  • November 12, 2016 at 12:57 am
    Permalink

    But where individual functions go? It would help for the newbies like myself!

    Reply
  • November 21, 2016 at 6:44 am
    Permalink

    Thanks a lot buddy,

    BTW,
    Don’t we need to create Editor > NSManagedObjectClass?

    Can we just go with these 2 functions?

    Reply
  • Pingback: Core Data for Swift 3 and iOS 10 Learning Notes | Yao Li — Programming, Reading, Running, Thinking, Traveling, Living

  • December 13, 2016 at 11:39 pm
    Permalink

    Thank you, this was very helpful

    Reply
  • December 15, 2016 at 5:54 pm
    Permalink

    Gracias. Thanks a lot!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This

Want this Post in Easy to Understand Video Form?

Then get the Complete iOS Developer Course. Over 50 hours of pure Swift coding goodness designed to turn you into a coding master!

One Time Offer - $9! Click to Get it!



WARNING: Once this box is closed the offer will not be shown again.

Thank you!