Beginning Core Data - Part 1: Introduction | Ray Wenderlich

What is Core Data? What does it bring to the table? This introduction will give you an overview of this powerful framework.

This is a companion discussion topic for the original entry at


Thanks for very nice video. I learned a lot on Core Data and the related Fetch Result Controller delegate.
But …
I implemented in section mode and the whole application crashes in all cases when sections are modified:

  • creating a new section by generating the first element of a section
  • deleting the last element of a section, which should result in delete of section
  • modifying an element that generates a switch of section.

I get the typical error : Invalide update : number of rows in section 0. Before update 1 , after update 1 but with one insert.
I have the impression that creating a section generates two entries in my tableView.

I tried to use the extension : didChange sectionInfo with edit / delete but it does not work at all.
func controller(_ controller: NSFetchedResultsController, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)

Any idea where this going wrong, or any planned update of your course for this case ?

Luc Lannoo

I have a vague recollection of the sort order for sections not matching the sort order for the fetched results controller causing issues with the section handling. There might be another thread about just this issue. So check to see if the sort order matches the section order and if not, please try correcting that.

If that doesn’t work and you are using any custom code, please post your code somewhere online and provide a link and I’ll take a look.

Take a look at this thread to see the change I’m talking about (though I’m not absolutely sure if that is the same issue you’re seeing):


Thank you for this reply. I looked to the discussion on lesson 13 and I confirm I have the same problem.
In attached link you will find my custom code : Dropbox - File Deleted

I have added in the init a procedure to init the view.
My problem is

  • how to change the text so that the entry moves section
  • how to add a fully new text (this means a new section)
  • how to delete the last item of a section
    All the other functions seems to work so the problem is as soon as the section headers are changed.

Thanks for the help,

Luc Lannoo

The issue you’re facing is not the same one as in the thread I linked to :slight_smile: Your section key and the sort order key match. Your issues arise from multiple issues as far as I can tell based on a quick look at the code. However, since this is a totally different app, I’m afraid that troubleshooting it would be outside the scope of providing support for the original tutorials.

Thanks I will look and search further.


Just for information. I downloaded the material after Part 2 Lessons 13.
When installing the software on my Mac I have the same error, this version is not working.
Same problem. Init of a new section is not properly implemented.

Only when the core data is filled in in the previous steps of the tutorial, this means following all the steps from part 1 till part 2 lessons 13 it is working. It does not work on a fully empty device.
(code : Beg Core Data S2 V9 Fetched Results Controller


I am not sure I understand what you’ve said about core data being filled. I assume you mean that if you run the project after the end of lesson 13 without having run any of the previous projects (and so, not having any data already in the app) it fails? I am checking on this right now and if I find an issue, I will get back to you but would like to clarify what you meant if possible …

As an update, I can see the issues you mean, but they all appear to be related to incompatibilities in iOS itself. See the following post:

That’s the first issue you will run into when trying to add data in an empty app. This appears to be due to a bug/issue with UICollectionView and CoreData that has not been fixed by Apple still.

You can get past that by doing what I consider to be a slight hack - instead of inserting just the needed cell when you detect a CoreData change, you can call reloadData on the CollectionView. This is wasteful in my opinion but it works.

But then, when you create a new section, you will get another crash. But that crash (at least on the surface) does not appear to be with CoreData but has to do with this issue:

And I don’t have a solution for that yet but I’m looking. Quite honestly, I would not use CoreData for anything but the most trivial project. I personally use SQLite directly rather than use CoreData due to all the issues that seem to crop up with CoreData and are not fixed by Apple. But I realize that this does not help you … so will post an update if I find a solution to the second issue.

1 Like

OK, the second crash went away after I converted the UIColor value to a string value. Everything works as before and there are no crashes even if you start with no data and continue to add data. Here’s the final project: (37.2 KB)

Yes ! This is what I wanted to explain. Download 13 works only if data on the app from previous lessons.

See the fixed version I uploaded in my last post. That will work fine whether you have data from previous lessons or not - it’s the same basic principle but a few things had to be changed due to various bugs/issues.


You gave me the answer. I changed all the updates of theViewController to reloadData and now it works fine.
It is also logical. Data is managed with the fetchedRC. ReloadData is just using that data to present it in the Viewcontroller.
I will now take your hint into consideration and start to learn the usage of SQLite.

Thanks for support,


Good luck :slight_smile: I wrote my own SQLite wrapper to make working with SQLite easier. If you’d like to take a look, you can find the source and the documentation here:

Not sure if that will help or not but I find that approach easier to work with than CoreData since I have more control over how data is accessed/loaded :slight_smile:

Is there a course that covers getting to the file this course starts with? I would like to get a better understanding of what I am looking at to start with. I notice even to start with the project is using 2 data files that I have not used in any other tutorials, mainly I would like to understand this better. I would love to go through a tutorial building the petal app before adding core data to it. I am trying to build a similar app to Petpal and running into a few hiccups that would be nice to have a better understanding so I could avoid them.