Kodeco Forums

Getting Started with Core Data Tutorial

Learn the basics of building the data layer of your iOS app with this getting started with Core Data tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/918-getting-started-with-core-data-tutorial

Why did you not use the new method with Xcode8 and CoreData?
The property class definition in codegen inside the xdatamodelid?

With that, the entity manipulation is easier like:


Is because this method is not powerful? I don’t know, I just started using CoreData.
What do you think guys?

Great question, arnaud512! In the book, we sometimes take the longer but more explicit way of doing things as a way to show the reader everything that Core Data is doing on your behalf. That’s why we do the manual Xcode generation and then call out the newer method in a separate note.

Hello Pietro,

In step 2 of the step-by-step description of the view controller method “save(name:)”, you write, “You can do this in one step with NSManagedObject’s static method: entity(forEntityName:in:).” But I only see a definition for the static method “entity()”. Can you help?

Thanks much!

Blayne

Hi Blayne, this is the method I was referring to: Apple Developer Documentation

I would be used in this way:

let managedObject = NSManagedObject.init(entity: myEntityDescription, insertInto: myManagedContext)

Very good. Thanks for the clarification!

When I try to use the line:
let managedContext =
appDelegate.persistentContainer.viewContext

I get an error that AppDelegate has no member persistentContainer. I initialized the app with Core Data checked, and appDelegate.swift has the persistentStoreCoordinator, but not persistentContainer. When I tried to add it manually, I get a warning that persistentContainer is only available for ios 10.0+. Is this true, or am I doing something wrong?

I had same problem. You should be fine to continue if you go for:

let managedContext = appDelegate.managedObjectContext

Hello, is it possible to update a record in Coredata?

is it possible to get a version of this tutorial in Objective C?

1 Like

In the method viewWillAppear, the text of the tutorial has the line

let fetchRequest = NSFetchRequest(entityName: "Person")

and the download code has

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")

The text of the tutorial will cause a syntax error, and the download code will correctly compile.

please get me first part of core data tutorial series in objective c i need it fast. please do share

Solved :slight_smile:

Do you have the full finished example for Xcode 7.3.1?

An NSManagedObject subclass is an object that represents an entity created in Core Data, whereas an Array is a collection of objects. Are you asking if the total collection of entities inside core data may be stored in an array, or, are you asking if a specific NSManagedObject may be represented as an array? Can you please clarify, and perhaps give a specific example, or scenario?

I solved my problem with a different solution. Thanks anyway!

This tutorial has helped me to get started using CoreData, thank you!

I am making a weather app and I am storing locations using CoreData. I have figured out how to read and add.

My problem is that on first app launch, there is no data and my app crashes. How can I pre-populate some data so I can read data on first app launch? In my case, I want to prepopulate some locations, e.g. “New York”, “London”, “Paris”.

This actually is a very interesting question that I personally had to deal with in the past :slight_smile:

The best way to do this is to run your app initially, and have your code at first launch trigger a preload function to initially populate Core Data with the relevant data you need. Once you do this, stop your app from Xcode, and then open up your app file that is running on your simulator, and locate the .sqlite file there (which contains the data you’ve just loaded). Take that file, rename it, and then replace the .sqlite file inside Xcode (which would be empty), and run your app again (however, you can either set a boolean flag value to prevent the initial preload function from running again OR simply comment/delete that block of code entirely). You should be able to now run the app and see the pre-installed data right from the beginning in your app WITHOUT having to run any function to preload any data.

Let me know if this helps! :slight_smile:

Awesome tutorial, you guys always do a great job. Quick question though… Mine all works fine (my implementation is slightly different as I needed to filter using a predicate also) but I see a warning in the console:

CoreData: warning: Unable to load class named 'Component' for entity 'Component'.  Class not found, using default NSManagedObject instead.

Like I said, it all works fine, but that warning. What’s that about? Thanks again!

@beck. Thanks very much for your question!

I found the following solution to a similar problem:

Go into your XCDataModel and make sure the “classname” for all of your entities is set to NSManagedObject.

I hope this helps!

All the best!