Episode #569

Inserting and Deleting records with the ModelContext

Series: Leveraging SwiftData for Persistence

6 minutes
Published on November 30, 2023

This video is only available to subscribers. Get access to this video and 584 others.

This episode discusses how to work with the ModelContext in a SwiftUI app to insert and delete data. The ModelContext can be accessed through the ModelContainer, which is set up in the environment automatically. The @Query macro is used to retrieve artists from the database and iterate over them. We also implement swipe to delete using the onDelete modifier.

This episode uses Xcode 15.0, Swift 5.9.

In this episode, we're going to take a look

at how to work with the Model Context in a SwiftUI view

so that we can insert and delete data.

So here I've got an artist's view.

And this is going to show me all of the artists

that we have in our database.

So I'm going to go up to the top where

we are going to be able to add in an environment called

Model Context, var model context.

Now, because we are inside of a SwiftUI view

that has the Model Container added--

and as a reminder, we can go over to our app here--

this added this Model Container.

And this Model Container is set up this way.

So we've got our Model Configuration Schema,

Model Container.

Anything inside of this hierarchy

is going to be able to retrieve the Model Context out

of the environment.

So here in the body here, I can work with that if I needed to.

Let's say I wanted to say color.clear.

And on a peer of this, maybe we have model context.

And we could do something with this if we needed to.

There's also the query macro that we

can use to say that we want to pull in artists.

And that is a list of artists.

This query macro is also going to retrieve the Model Context

from the environment so that we can

iterate over the artists that are listed in the database.

So we can say artist in.

And here we'll just say text with the artist.name.

And if we rerun our preview, it is actually

going to crash because our preview is not

set up with a Model Container.

So let's go ahead and do that next.

We're going to say Model Container.

And here we can say for and in memory.

So we're going to say for artist.self.

And in memory is true.

And while we're at it here, we can give this a navigation

title of artists.

And in order to see that, we're going to have to add a

navigation stack here so we can see it in context.

So we've got our artists here.

But we have no artists in our database.

So what are we going to do?

It's hard to see how this works without

actually seeing some data.

So what I'm going to do just for the moment is say on a

peer, and this is where we might decide that we're going

to create our first artist.

And I will create one called Pink Floyd.

And then here we can say model context.insert.

We can pass in the artist that we created.

And then we can say try model context.save.

Now you can choose to save later or periodically.

You don't have to save after every single change.

Because this is going to track those changes.

And until you save, it'll sort of batch all the changes you

made and insert them into the database all at that time.

So at this point, now we have our artist.

And if we want to have this in here all the time, we can do

that, we could also do that inside of our preview so that

we always have some preview data set up.

OK, so because our model container in memory is set to

true, then this is just going to be some sample data we can

work with just so we can see what's happening here.

And this is how we can both fetch a list of artists that

come from the database, which happen right here.

And it will automatically observe the store so that if

anything in this model context changes, that this query will

be rerun, it'll recompute this body.

OK, so let's add a navigation button, or a toolbar button.

Let's button.

So we're going to say add system image will be maybe

person.add.

And the action is going to be--

let's do the same kind of thing.

We will create a new artist inside of here.

And this will be Jimi Hendrix.

So now if I click add, you can see this new row.

If we want this to animate, we can wrap this in a with

animation block and move those things inside of it.

And now when I click add, it animates in.

To add a swipe action here, there's a couple ways we can

do this.

I'm actually going to switch this to make a list that

doesn't take any data, but inside of it has a For Each.

And that data here will be the For Each artists.

The content will be the artist.

And then we can move that inside, make

sure it still works.

And then here we can add on delete.

And that gives us an index set of the items that we want to

delete.

And in this case, it's just going to be one, but we could

say for index in index set.

And now we can say artists--

actually, we'll say model context dot delete, and we'll

get the artist at that index.

OK, so now if I add a few rows, I can swipe to delete

them, and that will delete them in the database.

In the next episode, we're going to take a look at adding

a form to this so we can insert records by filling out

all their properties as well as editing ones that we have.