Saving Transformable Types Using Core Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to adam shaw weekly and in this video i'll be showing you how you can save ns color or ui color into core data database as a transformable property and then retrieve it and display it on the screen so let's go ahead and get started now for this app i'm going to be building a mac os application so make sure that you are selecting mac os and we're just gonna select the app template you can name your application anything you want so i'm just gonna say rooms app and let's just save it on desktop now keep in mind that you can if you want you can also use uh the ios application that is perfectly fine but i'm just using mac os all right so the process will be very much similar so the first thing we need to do is we need to make sure that the user will have some place so that they can enter a room and a color all right you can see that on the right hand side i can see the preview of my application since this is a mac app it doesn't really show any iphone or anything it simply shows a window which is produced by our code on the right hand on the left hand side so let's go and get started in order for a person to enter a room and we are only con considering the name of the room and the color associated with the room and that's it so the first thing i'm going to do is going to add a v-stack alignment with leading and i can go ahead and say text adding a room font large title you can see that it's already making nice shape you can see add room now i do want to add a little bit of padding and maybe i can go ahead and provide some sort of a maximum width and some sort of a maximum height if you want okay looking good now in order to ask the user to enter a title of the room we need a text box so i'm just going to go ahead and add a text field and we can say the name keep in mind that for this application uh the main purpose of this video is to just show you how you can store the ns color or ui color into the sqli database using core data so we will not really be using any mvvm design pattern i have a lot of other videos that you can definitely check it out if you want to learn more about mvp design pattern and i'll also make a link or something in the youtube description so that you can check it out so here i'm creating the state property so that the binding will work correctly right there let's go ahead and refresh our preview so we should be able to hopefully see the add room text as well as the text field let's go and see if it works sometimes you'll see the preview doesn't really work as expected but there we go it's looking fine now i'm gonna also go ahead and push everything on the top by adding a spacer so this is great we can enter or add the name but what about the color i mean how exactly are we going to allow the user to pick a color for for a room now you can use the color picker if you want to but i'm just going to show you how easy it is to create a color selection view if you want to so i'm going to go ahead and add a new view color selection view okay and we should be able to see the preview which you can see that it doesn't really have anything so which colors do what colors do we want so we can create an array of colors and next we want to display those color in a horizontal fashion by the way i am a little bit under the weather as you can tell my from my voice here we go id self color in and we should be able to display an image which will represent the color so system name and i can go ahead and simply say circle dot fill so let's see what it displays you can see this place a white circle we can go ahead and make the circles a little bit bigger if we want to and we can go ahead and change the foreground color and let's go ahead and also clip shape to be a circle so the only thing we're looking at is a circle all right so this is what we have right now and another thing that we want to do is when you select a particular color so on tap gesture somebody is going to be passing in the binding property which means that whenever we select or set the selected color then the parent or whoever is passing down this selected color will also get updated selected color equals to color okay now if i try to run this it's going to complain because in the color selection view preview we need to pass in one of the arguments the only argument which is a selected color and i'm just going to pass in blue all right now one of the things that we really need to do is that when you actually select a color it would be nice that if you give an indication that the color is selected so what we can do is we can check over here if the selected color is equals to the color then we will go ahead and display record.circle dot fill else we will display something else and you can get all of these names from an application called san francisco symbols and you just need to put the code over here which are the name of that particular symbol and now you can see that since i'm passing blue the blue one is the one that is selected and that's it that's how you create a color selection view it's pretty simple now let's go back to our code and display our color selection view so color selection view will take in a bindable property of the color and currently we don't really have that which is not a big deal because in our parent which is our content view i can go ahead and create a property and assign it some sort of a default color and then send it back as the property perfect and make sure to have a var obviously so this means that the color selection view where you're passing in the color whenever you change the color this color property also gets changed in the parent which is great now let's go ahead and run the app and you'll be able to see the color selection view in action oh wow it looks really good you can see that looks really good and i can select different color and the one that is selected is the one that has a little bit of a circle inside the black circle but that's the one that is selected perfect now the other thing that we want to do is we want to obviously save the room with the color so i'm just going to go ahead and add a button and when we click a button we should be able to save that particular color to the database all right now we don't really have any code on anything so the first thing i'm going to do now is i'm going to go ahead and add a data model and you can call this anything you want i'm just going to call it room model there we go let's add an entity that entity will be the one that's getting saved to the database and i'm just going to call it room now we will move to the attributes of that entity attributes mean the properties so one of the properties we already know which is name and that will be a string and that is required so let's go ahead and make sure that optional is not checked there we go the other property is each room will have a color but this time we want to actually store the actual color since we're using a mac os application it will be ns color if you were using a ios application it would have been ui color now there is no type that says ns color or ui color but there is a type called transformable and transformable type is a type that can be transformed back and forth so one thing that we're going to do is we're going to set it to transformable it's not going to be optional and the custom class what this transformable represents will be an s color all right okay now when we build the application since the room entity is set to code generation it's automatically going to generate the files associated with the room entity and if we look at that file you can already see this is generated by xcode you can see that it had created a property which is of type ns color the problem is that the ns color does not belong to any of these modules and this color belong in app kit so you might be very tempted to go ahead and edit this file and add app kit but first of all you can edit this file this file doesn't belong to you and even if you find a way to edit this file the next time you build it will be overwritten so which is right here so we can't really do that so we need to find a different way to accommodate for our ns color property so i'm just going to copy all of this stuff and i'm going to go ahead and create a new file you can call this file anything you want this file will be our room file in other words we are asking core data that you know what don't generate any file for us we will just do it ourselves but this is not really going to work right now you still have to go to the room model select your entity and you have to say that i don't really want the code generation to work like this i don't want you to be creating classes for me i will just do it myself manually so i'm just gonna say manually which means i'm responsible for creating a core data file associated with my room entity go to the room file now you can't really have an extension on the room when there is no such thing as a room so i'm just going to go ahead and create a room ns manage object and since core data is mostly in terms of uh objective c we're just going to say objective c room so that it can be represented over there also apart from that everything should be okay the only thing we need to add to this file will be app kit so that we can use nscolor now it is possible that it builds successfully but it will still complain that's just xcode there we go it is perfectly fine because you can see that your build well right now it's not fine i guess but the bill says succeeded so you can see that there we go it says zero and then eventually it kind of comes back it should be okay i think if you close xcode and start over again it should be okay but build has succeeded and it's just xcode having issues all right so this should be okay then the next step we need to do is to initialize core data we will create a core data manager i have done that number of times so we are not really going to spend that much time in initializing core data so let me go ahead and just copy the core data manager there we go the core data manager uses the ns persistence store to load core data that's it it does have a one property called shared and that can be used to access to the instance one single instance of code data manager and from there we can access the view context let's go back to our rooms app file the starting point of our application and we need to make sure that we have access to the context we will get code datamanager.shared.persistentcontainer.viewcontext and now we can go ahead and inject the environment manage object context and view context once again i'm not really using mvvm design pattern i'm just trying to show you how you can save the ns color as a transformable property into sqlite database using core data all right so now we can go back to our content view and we should try to probably save but before we do that we need to access the view context property so i'm just going to go ahead and refer to the environment and the key that we are looking for is manage object context view context let room equals to room and then view context room dot name equals to name room dot color equals to color well we can't really assign color because the color in the room is ns color and the color that you're trying to assign is just a color structure so no problem we can go ahead and pass in the color to nscolor and we will be good to go finally we can call viewcontext.save which is actually going to save our data to the database let's go ahead and run the application now as soon as i run the application and i haven't really saved anything i get this weird big message and basically this message is saying that looks like that you're using a transformable property but you forgot to use a transformer which is a necessary thing that you need so that you can transform the data from data to ns color and ns color to data so even though we have set that in our room model that the color will be of type nf color we still have to create the transformer which will transform the ns color to data and data to an s color back and forth so let's go ahead and add that new file you can call your transformer anything you want i'm just going to go ahead and create a very descriptive name and this color transformer and in the ns color transformer let's go ahead and import app kit because we will be transforming back and forth from data to ns color and ns color to data we will create our class called ns color transformer which is a value transformer now there are a couple of different functions that we need to implement there is a transform value this is the one that will transform it to the data and then the reverse transform value this is the one that's going to convert from data to nscolor so the first thing we're going to check over here in the transform value is we are going to get the color we will convert or try to cast the value to nscolor if it fails then we can simply return nil if it doesn't fail then we can probably go ahead and archive it so nskeed archiver dot archive data we are archiving over here color required secure coding is find true so that we know that when you are encoding it at that time you are encoding the same object and you're not allowing other objects to jump in try let data and since the nsk transformer can actually throw that's why we're wrapping it up over here and we can return data on the other hand if you do catch an exception then we can go ahead and return nil the same thing we need to do but in reverse order in the reverse transform values i'm just going to copy paste the code over here which is basically taking the data and converting it to the color even though we have created this we have to still set this property so let's go back to our room model select color and make sure that over here where it says transformer you are adding a name of your transformer which is ns color transformer apart from that you also have to tell your core data manager that you are using transformer so let's go to the core data manager and right over here we can go ahead and say value transformer dot set value transformer and the name of the transformer is nf or the actual type or instead of endless color transformer and the name of the transformer will be whatever you want to give it i'm just going to go ahead and put the same name and there we go all right so let's go ahead and run the app now and it's nfl transformer was found for attribute color no value transformer with this transformer name um [Music] okay let's see what i'm doing wrong i think it was just a name so this was ns color transfer instead of transformer so i just changed it hopefully that will fix our issue let's go ahead and run that app again and looks like it's good now i can go ahead and add a particular room so let's say that i would say kitchen and i will color it to red and save and then i will say bathroom with purple save now one of the things that will be really nice is that if we are able to uh display it i'm not sure what that error is i i think it's not related to what we are doing hopefully but what we want to do is we want to also display our all the results so let's go to content view keeping in mind that we did add couple of different rooms with the color in order to fetch all of that stuff i can use over here the fetch request i can provide entity which in this case can be room.entity sort descriptor can be nothing predicate we don't care about and this we don't also care about i'm just going to say all rooms this will be fetch results and of type room once we have this we should be able to simply go ahead and display it so since it's going to return multiple rooms i can go ahead and iterate through it and try to display it in a list etch stack text room dot name let's go ahead and add spacer and for the color we're just going to have a rectangle and we will fill it up with the actual color that is that is provided to you so we will say color and room.color dot frame max width and we can say android over here uh i've used force unwrapping uh there's no way to i guess save a color as a nullable so if it's not saved in the database as null so it must have a value so that's why i'm doing that if you want to unwrap it you can do it in a different way if you want let's go ahead and run it and there we go we are getting a kitchen and we are getting a bathroom over here let's go ahead and say kids room 1 and i'm going to go ahead and name it as blue kids room 2 we can do yellow perfect so now you can see that we are able to to save ns color as a transformable property into our database using core data so hopefully you have enjoyed it make sure to download the github repository and thank you so much for watching hey if you like this video and want to support my channel then the best way would be to check out my udemy courses i have a lot of different udemy courses ranging from testament development sif ui but if you're looking for core data i have the best selling course on core data which is going to also cover the stuff that we just covered the transformable types but it's going to cover a lot of other stuff one-to-many relationship many-to-many relationship uh cloud kit icloud integration and a lot of stuff using core data so all the links are right there in the description of the youtube video and i really hope that you enjoy the courses thank you so much
Info
Channel: azamsharp
Views: 502
Rating: undefined out of 5
Keywords: SwiftUI, ios develppment, ios developer, core data swiftui, core data, swiftui tutorial, swift, swiftui 2.0
Id: g6jBU8E1K2U
Channel Id: undefined
Length: 25min 36sec (1536 seconds)
Published: Tue Nov 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.