Swift UI Simple Core Data App Tutorial (Part 1)
Wanna make a Core Data app but don’t know how? Tried of tutorials that only show you a piece of an app? This tutorial will show you how to make a fully functional app that connects to an API on the internet and utilizes Core Data. And we will do it following the MVVM standard programing pattern.
Introducing Cat App.
Setup the Data Models
Create a new Data Model file. Name it something you will remember. I always call mine “Stash”.
Add a new entity. For this project, let’s use cats. I like to include the word “entity” when naming my core data entities. That way, I can always tell in my code when the class is a core data model.
Add these attributes. ID as UUID, age as Integer 16, favoriteFood, name as Strings, dateCreated as Date, and finally picture as Binary Data.
Write the Data Controller
Now let’s write some code. To make things very simple, we are going to use the singleton pattern for the data controller. This will make one object in the entire app that will handle saving and fetching from Core Data.
Here is our beautiful Data Controller. Now we can use Core Data from anywhere in our app. But, it doesn’t actually do anything yet. Let’s start making cats.
Make some cats
Let’s build a simple form. Simple but with one caveat. Because our phones are probably not filled with pictures of cats, let’s use a simple API to get some.
First, the API Manager
Get a a free API Key from https://unsplash.com/developers
Then, write the model and API Manager for getting free images from Unsplash.com.
In the future, when the Unbounce API changes, you may need to change the UnsplashPhoto codable model to correspond with it’s json response.
Then, the cat views
Now we can write the views for adding a cat.
This is our form for adding cats to Core Data.
And here is the View Model where all the action takes place. All that is really happening is, when the view loads, the init will trigger and tell our API Manager to pull in 3 images.
Let’s make this a sheet on our ContentView.
Save cats to Core Data
Now that we have a form, we can try and save cats, with text and an image, to core data.
We need to grab pictures from the Unsplash API, turn that into binary data, then save it to Core Data. On top of that, we must do this asynchronously since we are connecting to the internet and waiting for data.
Let’ start by writing a little bit of bad code.
This extension to UnplashPhoto will let us asynchronously download the image of that cat we choose. Normally, inside each of these places where I wrote a fatalError you should write clever well architected code. But in our case, I’m going to say that if our image can’t download, the whole app is broken. It’s lazy, sue me.
Unsplash has a small hourly limit to how many images you can pull. If you are playing with the app a lot, you will start to get 400s and the app will start crashing here. Challenge mode- Stop the app from ever crashing
Save button
Now let’s go back out view model and write a save function.
Now we can finally write our save button in the NewCatView. We need to wrap our code in a Task since this stuff happens asynchronously.
Fetch the cats from Core Data
If we run our app now, we should be able to fill out a form, grab an image from the internet, and save it to Core Data. But is it working?
Our logs say yes! Let’s prove it. All we need to do it write a method in our handy dandy DataController to get the cats.
Now we can use that somewhere. How ‘bout a scrolling list of all our baby boys.
Since we are following the MVVM pattern, let’s start by making a new view model for our ContentView.
Kay, now let’s snap that into the ContentView.
Great! We have a scrolling list of all the cats we make. And Swift UI makes it super easy to put the button on the bottom of the screen by using ‘alignment: .bottom’. Now let’s make it look nice.
Add this code to our ContentView.
And that’s it for this tutorial. We learned how to connect to an API, download stuff, save it to Core Data, and then display it. On the next episode we will learn how to select, edit, and even do some animations. Source code will also be available in part 2.
P.S.
I’m broke, jobless and starting from zero trying to get a job as a iOS developer. Subscribe to my blog if you wanna root on, or if you’re in the same boat and wanna pursue this goal together.
Thanks so much for reading. 🐶 バィーバィー