Hi, Alex here from rebeloper.com and welcome back to the channel. T oday we are going
to build out a SwiftUI application with iCloud. So if you want to build out this
app, go ahead and keep on watching. Further more hit the subscribe button so let's
just jump right into it. So let's take a look at the project that I have created
for this awesome iCloud with SwiftUI tutorial. Now go ahead and click the link
down below to take a look at this project and this is a starter project
and let me just guide you through real quick so we can get into the meat and
balls, okay. Let's go into our SceneDelegate and here we have a list of items and
that is basically set as the environment object. Now if you don't know what
environment object is go ahead and take a look at my previous video and you will
also find out a lot more about states there. So this list elements is basically an
observable object and it has a published variable of items. Now this
holds the list of items that is what we will have a list of items that we will
save onto iCloud. Now let's go to our list element. Now this list element
of course you need to have it as identifiable because we are putting it
into a SwiftUI list and it has an ID of course as a UUID this is needed for the
identifiable protocol and it has a text. Now it also has a record ID and that is
optional and this is needed for CloudKit. We will see that in just a moment, okay.
Finally let's take a look at our ContentView and here we have our environment
object, list of elements and some private state variables, the new, show edit
text field, edited item and this will be, this will be used in order to save,
modify, delete and fetch from iCloud, okay.
Now let's see what we have here. First of all we have, let's just build and run and
it's real easy to say that. So we have a navigation view with a navigation bar
SwiftUI with CloudKit we have a text field here and the button add. This is
where we will add our new elements and then we have this double tap to edit,
long press to delete. Well here I'm just saying what you can do and these
will be on this list. I don't know if this is clear we have these lines we
have this list here. So that is what this view is all about. So here we have our
text field, our add button then we have our text field that will be shown to
edit the item, then we have the double tab to edit long press to delete text
and finally we have our list that has an ontapgesture and an on long press
gesture recognizer, okay. And then we have some animations and a navigation bar title.
Finally we have an own appear and that is where CloudKit helper will
fetch our items basically. Now this CloudKit helper, let's just jump right
into there. These are just functions that complete with an error or some other
type of success and basically that's it. This is where we will do the most of the
work and let's just go back and show you where these are used. So if you go you
can see here saving to iCloud. Let's just take a look at that. So we are saving the
new item. That is from the list and we are getting better result. If the result
is successful then we add that new item to the list elements items. So we are
inserting it at index 0, otherwise we are just going to print out the errors
localized description. So that is what we are
going to do in today in this video. Otherwise we will also modify, delete and
fetch from CloudKit. That's for the next episode of this series. So what do we
have to do first? Well first of all I already have this enabled but you have
to do the enabling on under signing and capabilities and if you go and tap on
this plus button that says plus capability let me just show you just
search for iCloud mine doesn't search it because we already have it
added here and make sure that you have enabled CloudKit and this iCloud dot
and your bundle identifier. Now in here may there may be a lot more but make
sure you are choosing this one, okay. And of course you have to have a team and
automatically manage signing. So that's it. Now let's go to the CloudKit dashboard.
And that is pretty easy. Just go ahead and click on CloudKit dashboard and you
will be taken maybe you will need to sign in. Let's enter our passcode here.
So there we go. Let's just search for our let's say SwiftUI, there we go.
Let's select this container and here you will see all of your data your schema
and so on. Now let's go back to xCode because we are going to see xCode, we are
going to add some code here. Let's go to our CloudKit helper and as you can see all
of this is nicely set up so we have a separation of concerns. We are dealing
with CloudKit logic here. So first thing we need to create here is a struct and
that is a record type. So struct record time and that record type will be
a static let items. So static lad items and items.
Now we will use this record type to create CK's CloudKit record. So today we are
going to focus on and this function that is save item with a completion.
Now we add a list element and for the completion we might get back that list
element of course and we might get back an error. So we are handling that inside
our content view, okay. So first thing first let's create our item record.
So let item record equals and this will be a CK record
and record type and that is what we have created here a record type
dot items, okay. Pretty straightforward. Now why do we need this item record?
Well because we want to add some elements on to this item record. So item record let's
see text because that is what we are going
to add. We are going to add a text here and that will be equal to the item that
is the list element dot text and we want to save this as CK record value.
So CK record value, okay. So now that we have our item
record now it's time to save it and that is pretty simple. But first of all we
need to know about the public database, the private database
and shared database. We are going to save and fetch from and to the public
database basically that is on your account and it can be accessed
throughout the whole apps users. Now for the public of course that will be on the
user's iCloud account and it will be on their database and that will be accessed
only through that user. Now with the shared you can share some but we won't
go through that we are just going to save now on the public database. So let's
start off with this CK container dot default and let's add in here our public
database, there we go. And let's have our save operation and we are going to use
the one with the records. So save record and we already have our item record here.
There we go. It is a CK record and we have a completion handler which has a CK
record and an error returning. Now both of these are optional so we need to
guard let unwrap those. So let's see record
and error, okay. First of all I will if let
unwrap our error equals error then we want to complete with an error. So that
is a pretty simple. So completion dot failure and we are using a swift 5
result so that is why we can use this failure and the failure error will be
error. So let's return just to be sure and then we want to guard let unwrap our
records. So guard let record equals a record else. So if there isn't a record
we want to again complete, completion dot failure and this time we want to use
these already created CloudKit helper errors. So CloudKit helper error dot and
record failure. That is what it is. So now you know why we need it this
enum of our cloud hit helper errors and we want to return. Now that we do have
our record now we can construct our list element that will be completed as a
success. So let's see first of all let's take care at our record ID. So let ID
equals and I will just call upon this record the dot and ID record, ID it is.
So we do have our idea. Now it's time for the text and that we need to guard let
unwrap our text equals record and let's see the key will be text and we need to
cause this as a string. Otherwise we want to complete, completion dot failure and
the error again will be an error that we have created cast
failure and that is CloudKit error and
record failure, cast failure, sorry. There we go. And we want to return.
Okay! Now it's time to create our item.
So let item our element or whatever. Let's call it element
equals, this will be a list element and as you can see we have two options and
we will use the one with the ID record ID and text. So we can omit the UID
because that is already added by default and the record ID will be this ID that
we have created and our text and finally we can complete. Completion dot success
and element, there we go. And that should be it. Let's hit command B and see if all of
this is okay. Oh, we need an else here. There we go. Hit command B and everything
should be okay. Build has succeeded, great. So before we test this out let's go to
our iCloud dashboard and select your development and your schema. If you're on
data just choose schema. There we go and you should see a custom
text field if not then go ahead and create one like add text field and make
sure that you have queryable, searchable and sortable. So make sure the text
custom field is a string and all of these indexes are added there. You can
also edit the indexes if you wanted to. Okay! You can add an index and so on.
So once that is out of the way let's just go back to our xCode project and build
and run and hopefully we will be able to add some new data of course it will not
populate the list but on the last we will have it on the iCloud dashboards.
So let's add in here Alex. Let's just click on add and yeah it says and this is
really important it says error saving record and this is quite confusing
create operation not permitted why we did allow it. So what you need to do is
just go into your simulator or rather yet your
simulator or your device. If you are checking this on a real device and go to
settings so you need to be logged in with the with this iCloud account.
Go to settings, let's sign in. Okay! Now that we are signed in let's go back and try to
save that into our CloudKit dashboards. So let's type in Alex maybe. Let's click
on add and it says successfully added item. Hurry! And of course it did update
the list because and let me just show you why. Under the content view once we
do get back a success for our result. So save item new value, we get back this new
value and we insert that into our list elements items array. So it says that
this is going to be on the background thread. So it is not allowed, so let's
create our save CloudKit helper on the main thread. So let's go back to the
CloudKit helper and right here let's go and type in dispatch queue, there we go dot
main dot async and here we have our code. Let's copy that out.
And that should fix it. There we go.
And build run again and hopefully we will not have anything under the content view.
Let's see, let's add of course we are not fetching and that's why we are
not having that Alex populated here but let's see rebeloper and let's add that
in there, okay. Successfully added item. Okay and it has been updated and now
let's check under the content view. Yay! No background thread needed. Now let's
take a look at our data. And let's see public database checked.
Zone, default zone, items. Yes! Filters none and let's quietly
our records. And if you can see I have a few of them already here and here we
have our rebeloper, our Alex and I believe I did have this saved two days ago as you
can see so yep that is already in the database. Now in the next video we are
going to fetch, delete and modify. So really, really interesting stuff coming up.
I hope you enjoyed this video where we managed to save some data into
iCloud with the help of SwiftUI. How awesome is that
so I want you to hit that notification bell and subscribe to the channel
because in my next video I am going to not only save some data to CloudKit.
We are going to fetch, delete and modify some data with the help of course of
SwiftUI. Why do you wait for that go ahead and check out these two videos too.
I talked a lot about SwiftUI, app development and iOS development in
general. So make sure to check out these videos too and as always I will see you
in the next one.