Core Data (Quick Start) in SwiftUI

Apple has a reputation for creating products that “just work”, and that may be true if you’re a consumer whose requirements are met by using apps on an iPhone / iPad. However, in order to create apps that “just work”, the App Developer needs to tussle with many things that don’t “just work”. At least, they don’t unless you know the mystical spells and arcane magic that Apple embues their developer software with.

Core Data

One area of major tussle comes from the mystical art known as Core Data. Wrapped within tangled incantations of NSPersistentContainer, and weaved around witchcraft spells such as NSManagedObjectContext, CoreData often feels like you’re entering the world of Dr Strange and the Halliwell sisters with neither a Book of Shadows nor Cloak of Levitation to help you out. Although, ironically, it does often feel like you’re using the Eye of Agamotto to keep going back in time to try and fix what isn’t working in your code.

Into this mystical world of confusion and strangeness comes the Ancient One who teaches you …. no, okay, enough with the Doctor Strange references. He’s not the ‘Ancient One’, he’s Mark Moeykens of Big Mountain Studio.

Anyone who’s read my previous blog posts (I think that’s probably just my dog) will know that I greatly appreciate Mark’s presentation style in his “live series” tutorials. I’ve only recently come across his books – primarily because (i) I don’t have the funds to purchase them, and (ii) he’s just had a fantastic WWDC-related sale on his website.

(No, there isn’t a physical book!)

Mark has been working on his latest book, Core Data (Quick Start) in SwiftUI, over the last couple of months and he invited feedback during the creation process. Rather than bewilder the reader with lots of text and complicated terminology, Mark brings his style, that combines brevity with a single focus on the key concepts, to break Core Data down into “just those bits you need to know” in order to get started.

I’ve previously followed Chris Ching, from CodeWithChris, through his iOS Databases Course, which covered Core Data, but I admit that I got a bit lost at times. There just seemed to be so much code required just to do basic things. Mark takes a different approach, and cuts everything back to basics so that you can “see” the essentials of what’s required to get you started with Core Data.

Core Data (Quick Start) in SwiftUI is a visual guide that takes us through each of the key concepts of Core Data as images with guiding arrows.

If this still looks baffling, turn back a few pages and go over it again!

The key concepts, in my own translation of Mark’s “4 steps” is something like this:

  1. Model (the bit in Xcode where you set up Entities and Attributes to “model” your data);
  2. Container (the bit that holds the data on the phone, so it’s not deleted when the app is closed);
  3. Context (the transitory bit where you fiddle with the data before committing it to the container);
  4. View (what the user sees, and all the other View code).

It’s not until we’re halfway into the book that Mark presents us with some code. From that point he guides us through three examples to show how it’s all put together..

The first example sets up the “persistent container”, attaches the “managed object context” to the environment modifier, and creates view code to display data from the persistent container utilitising @FetchRequest() and to save data with a button. (Hint: Page 59 & 60 summarises all of this in a handy “printout-able” way.)

The second example demonstrates an extremely useful method of incorporating “mock data” into your code so that Xcode‘s preview can show that our code is working. I’ll be using that a lot.

And, finally, the last example tidies up the code required for handling nils (if that sounds baffling, it’s important to remember that most properties (attributes) set up Core Data models are optionals and so you need to handle the likelihood of them being empty / nil otherwise you’ll get errors).

I took Mark’s final example there and used it to add in some mock data using the previous example so that I could see it all “working” in front of me, and I’m pleased to say that it all worked extremely well (my own newbie stumblings notwithstanding).

In the final word, this is an extremely effective introduction to Core Data. Give yourself a morning, read through the slides one-by-one, pop back and forth as needed to clarify some of the concepts, and then work with Mark’s code in the second half of the book, and you’ll be much further along the road than you are now.

I don’t promise that you won’t still be baffled by how some of this works by the end of it, but that’s on Apple and their World of Mysticism and Black Arts. What will happen, though, is that your mind will start to visualise the four “steps” of Core Data and, if you keep your version of Mark’s code handy, you’ll easily straddle the seemingly impenetrable initial hurdles when it comes to utilising Core Data in your own projects.


COMING SOON!

Mark’s follow-up book, to come later, Core Data Mastery in SwiftUI, is doubtless going to guide us through much more of the complexity of Core Data when it comes to Creating, Updating, and Deleting data. But don’t wait for that one…

Familiarise yourself with the basics with this Core Data (Quick Start) in SwiftUI guide first. By the time the follow-up book comes out, you’ll be ready for it.

Visit Big Mountain Studio today – you won’t regret it!

%d bloggers like this: