Uncategorized

Did you ever had the feeling that you want to pull out your hair because of CoreData? Well I did, still I made this tutorial series on CoreData and this is episode number two of the series and we are going to dive deep into CoreData attributes. So hit that SUBSCRIBE button, open up xcode and let’s dive in So let’s jump right into CoreData and some awesome attributes that you will learn today We are going to build out a task manager. So we can add tasks and edit them and complete them so, yeah that will be really, really awesome and yeah just so you know CoreData isn’t that hard. We are going to tackle this in this series. This is the second part of the series and make sure to check out the link in the description where you can find the link to the whole playlist and of course to the resources because yeah we are not going to build out the UI from scratch, it doesn’t really make sense and it will be a long video anyway, so make sure to check out those down and while you are at it make sure to smash that LIKE button So we can help other developers like you with the YouTube algorithm. So they can find this video. While we are at it let me just remind you that I do have at rebeloper.com/mentoring Something called DEV_FACTORY That is mentoring, we can chat on Zoom on your problems and by the way, if you have any issues with CoreData go ahead and check that out. Apply now and let’s just dive right into it by starting a brand new xcode project. So let’s just start off Create a new xcode project and I will name this appropriately in the previous video we have named this CoreDataFun1 and today we are going to call this CoreDataFun2 because, yes CoreData can be fun and I will make it fun for you to use So CoreDataFun2 Now we also in the previous video we used CoreData so we are going to do this right now So we are not going to use CoreData in the next episode. So make sure to absolutely smash that LIKE button so you get notified when that comes out. So now we use CoreData. We are going to talk about that in the next episode How to not use CoreData, like the template one So let me just save this here with a git repository created So you can take a look at it in detail once it is finished. So the link will be also in the description with the resources. So as usual let’s just do some house cleaning really quick. Let’s get rid of the main interface there. If you followed along my tutorials you already know the drill We are just going to do this really, really quick I’m not even going to go into why I’m doing it and how I’m doing it I’m just simply deleting them main. storyboard to do everything in code. So let’s go here window scene, there we go And what we want to do here is just initialize the view controller and that is a pretty straightforward to do Let me just scroll a little bit up so you can see So window equals UIWindow Let’s just see window, there we go and we are going to use window scene There we go, let’s just make this key and visible So window make key and visible and finally yeah we are going to use UINavigation. So let’s just use UINavigation controller here So window root view controller equals UINavigationController for the root view controller and while we’re at it let me just create our controller here So let controller equals our view controller, there we go And let’s just add that in there So let’s just select our simulator and basically just build and run and see if everything works okay. We should have a simple navigation bar at the top. Build has succeeded, that’s always a good thing to see and let’s just wait for this to load There we go, really, really nice, okay. So let’s take a look at the code. If you don’t really know

what’s going on in the app delegate with this persistent container and save context make sure to check out the first video in the series Now as I promised you we are going to do a little bit more than in the previous video in this GUI So we are going to create a task So before we do that let’s just import all the files from our resources So there we go, you should have all of these and we have an image of the model which I will use just for guidelines so we cover all the types of attributes for the entity, yeah Attributes entity, yeah go ahead and check out the first video, okay. So let’s just drag and drop, first of all let’s delete the view controller because we are going to remove that and replace it with a new view controller. So let’s just go ahead and select all of these except the model So drag and dropping it right over and there. So make sure copy items if needed is checked and the CoreDataFun 2 is checked Now if you take a look at the these files you will see that I’m using something called SparkUI. That’s a library that I created for building out UIs really, really fast. So go ahead and check that out If you don’t want to then just create this table views or collection views on your own the gist of it is the same, you will learn CoreData So for now let us just import inside the swift package SparkUI. So SparkUI, there we go Hit return there so we can see let’s select that one. Let’s click on the next there and yeah let’s just use the branch version to be exactly 0.11.11 because yeah most probably I will add some stuff to this repository and maybe it will break something So this is the working copy make sure it’s 0.11.11 Now this will take a while to load because it’s fetching all of the dependencies and it depends on your internet connection So we will see you in a while Hey, welcome back and with the power of editing this should have taken you no more than three seconds While it took me a little bit more than five minutes, okay So let’s click on finish here and we should be able to see all of our dependencies. Really, really nice So let’s hit command B to see if everything builds and if we have any issues then we should fix it right now and after that we are going to move on to the xcode template generated CoreDataFun Now again we are going to build all of this out like creating data model Creating CoreData stack in the next episode. So go ahead and hit that notification bell and smash that like button to get notified of that new episode when it’s coming up on this epic CoreData Tutorial Now everything built successfully so we can move on with the tutorial So let’s check out our CoreDataFun and let me just open up this model Well it’s a png file. So this is just a reference So what we are going to be talking about. So let’s see here First of all, yeah let’s just go one by one It doesn’t really matter I will explain everything So color and it is a transformable and we are going deep, deep by the way let’s just create our entity because as you can see we have a task here Now entities is kind of models Go ahead and check out the first video in the series, okay. So we are going to create a task here So task, there we go Make sure that it’s just tasked no spaces there And that is it. We have now available attributes So the first attribute should be a color So I’m using command tab here to go between these windows So the first one is color. So we should have color and as you can see there are no UIColors here and that is why we are going to use a transformable, okay And how does the CoreData or our code know that this should be a UIColor? Well we are going to set its custom class here under the attributes to UIColor So that is how easy it is As you can see it’s just set to UIColor and we have an attribute, it’s transformable and it’s transformable in a way

that’s mind-blowing, yeah really, really awesome Otherwise how would you do that, maybe you would just add int values for rgb like red, green and blue and maybe for alpha values, but that is so tedious Now with transformable you can do that really, really quickly and easily Of course we will see the code how we just manage all of this Okay, next one is completed, yeah because a task could be completed So completed, there we go and that is a boolean value, of course boolean value. It is it true or false and that is all you have to do here So let’s move on, I try to move this as a quick paste because there is a lot of code involved So created at. So yeah maybe we want to have created at because we want to have a sort like ordering, not sort, ordering according to the creation date and yeah that is a simple date. So after created at let’s take a look at the difficulty, yep because we want to add a difficulty from one to five and that difficulty will, yeah it’s a good thing to have. We are just going to printed like show that out in a label So integer 16, 32 and 64. Well I prefer integer 64 because yeah we are on a 64 bit processor. So it doesn’t really matter in memory So difficulty integer 64 Id, yep every object should have an ID Let’s have an ID here and that will be a UUID. Now this is if you know UUID that is a long random generated string from the UUID string of course because that’s UUID, not a string, it’s a UUID. Image data Yeah! That is really interesting because we want to save image data. So we cannot save UIImages We can only save binary data. So we want to save image data and we are going to convert UIImage into png data and data into UIImage So one thing that you should add here for the binary data is allows external storage and CoreData will take care of it. So yeah you don’t have to load all of this into memory all over and over again. So allows external storage that is really useful to check here. Now this attribute is not available on all types. I believe it’s only for binary data but yeah make sure to check that also So image date and finally title You already saw title in the previous episode. So title Challenging for me to type today and if we just take a look at all the other it’s a double, decimal, undefined ,float Yeah URI it’s just URLs If you want to but in most cases you store URLs as a string and then convert it to an actual URL in code. So I’m not going to cover that but you can just store URLs in as a URI type also Okay, or just a path to a folder that’s also URI Okay! So now that we have this let’s talk a little bit about codegen because if you just go to task you will see under class you have the name of course task and here’s something codegen and let’s just take a look at the files. So we don’t see any classes and if you recall in my previous video I told you that if you just, by the way it’s selected by default if you select class definition that will generate classes for you like the actual code and the classes for you for these entities and for all the entities. Now that is kind of hidden. You cannot think in with it but in some cases you do want to think in it and this is the place when we are going to well not think on it, we are just going to create it But let me just show you an error So first of all let’s just build and run this and yeah, build has failed Let’s see why it has failed Yep use of undeclared type UIColor Okay, yep and this is why it should be. As you can see there is a UIColor but in the generated task dot plus CoreData properties we didn’t import UIKit Well by the way CoreData doesn’t know this So we want to generate a new file, but anyway just because we have build

and run coordinator generated these files already. As you can see it’s task.core data properties and you will see there are two files. One for class and one for this extension actually, we are in the extension. So anyway let just move forward and now we have this inside our xcode project just so you know But we want to have it as a manual or a none. So now that we selected this and hit command B nothing will really happen, of course we have that error because we do have, but we now have to go under editor and create NSManagedObject subclass So let’s click on that and select our CoreData model and select task If you have more entities then select all of them or whatever you want to create these files for and let’s click on next there Yeah, this is perfectly fine. So let’s create them So now you can see that these two files a class and the properties file basically the properties in extension on that class has been created. So what I usually do is grab and drag and drop them under my CoreData model or inside the file folder system whatever, so they can be close to each other Now let’s just take a look at these two files. First of all we have a class. That is the model for our task and as you can see it’s public class it is an NSManagedObject Take a look at the first video if you don’t know what I’m talking about And then we have an extension on that class and as you can see it says UIColor Okay, So what you want to do is just hit command B and we will have an issue here As you can see it says that build has failed use of undeclared and so on So what we want to do here is import UIKit, okay. Let’s hit command B again and now build has succeeded How awesome is that! But remember that auto generated file is still there, so we will have trouble with that later on but what you could do and I will just fix it already. If you encounter that yeah you have multiple files in the derived data. Make sure this is the keyword in that error warning. Derived data, there are multiple files into derived data Make sure that you just go under product and clean build folder so that will clean that also and while you are working with CoreData make sure that you either delete the app from the simulator or to be 100% sure go under edit, device yeah under, on the simulator you can’t see this right now but yeah device erase all content and settings and let’s just erase everything That will erase everything from the simulator which is really what we want to do Okay! So yeah it’s reloading but I still have some stuff to explain to you so. Now that everything is working okay, as you can see we have color. We could add anything into this extension file So whenever we recreate all of this it will recreate only the extension file So if we wanted yeah I just told you to add new stuff here. You don’t add stuff over here So yeah that’s what you want to do Now if you want to generate only the extension file then you should choose right over here, let me just select the entity, not manual or none, category slash extension So yeah you could just experiment with that I usually do manual and none and just override everything and make sure yeah it will just do its stuff to generate the class and the properties. It’s up to you, well basically in this tutorial series we are not going to touch any of these classes but hey now you know how all of this is done, okay So now that we have all of that let’s hit command B so we know that everything is building okay and move forward by adding CoreData to our app and the first thing is that I want to have yeah by the way take a look at all of these files if you wanted to but yeah by the way let’s just build this out Let’s just wait this and let’s see how all of this is kind of looking here

because it is a stable to-do app But it doesn’t uh do its persistence So let’s click on add there and let’s add in here do yoga Let’s submit that, yeah and it’s not saving it Yeah but we will see that in in just a moment So we will see that in action, what we will have is just simply the ability to tap on a cell and then just view that and here under the ad we will be taken to the ad view controller, so we can add a little bit more than just a simple title So yeah that’s the basic of it and now what we want to do is create managed context. So that’s the first thing that we want to have on our view controller and yeah that is an NSManageObjectContext and we will do that right under here, under properties. So var managed context and NSManaged yeah and we want to import CoreData. Let’s see why isn’t it Managed context and we just banged it out Okay! So that is really, why isn’t it Oh it’s it’s not managed context but anyway it’s a good thing to close manage the object context, but now we have auto completion back on so yay So manage the object context and I want to have this manage context on the view controller because I want to pass it along to the add view controller and to the details view controller. So while we are at it let me just copy this out and add them as a property right over here and right over here, yeah we already have this and I just accidentally did not delete this line Okay! So we have that there So how do we add that into our view controller? Let’s go and do the scene delegate and basically we want to create a managed context from the appdelegate Okay! And what we are going to do is just first of all grab the app delegate. So let app delegate equals and that is UIApplication UIApplication dot shared dot delegate and let me just have some room here so you can see a little bit better And that should be an app delegate There we go. Now that we do have our app delegate we can grab our managed context Let and if you don’t know how this works make sure to check out the first video on the playlist. I talk a lot about this like 50 or 40 minutes. Manage context equals delegate dot and that is I believe it’s persistent container, yeah persistent container Let’s hit command B Oh this needs to be as As appdelegate sorry about that So persistent container. Now we have our view context, yep correct, yeah I just got lost a little bit but let’s move over so now that we have our managed context we just add that onto our controller managed context equals managed context and now you know why I just created this controller outside of our UINavigationController initialization So yes now that we and that is all you have to do in the app delegate So let’s move forward and let’s take a look a little bit more closely on the add view controller. Rather yet let’s just finish up the add button in the view controller because yeah we are going to present the add view controller. So there we go currently we are just simply showing this alert and appending the to do’s, the loading, action view, yeah it doesn’t really work because yeah it’s not persisting data in CoreData So what we want to do is create a controller. So let controller equals add view controller, there we go and view controller and we want to again move the manage context. So controller dot managed context equals

managed context Let’s just copy that out from here and paste it in and there and finally you want to call self navigation controller dot push view controller, our controller animated true, okay. And yeah this should work really, really fine. Now I believe we should add in here self, because we are in the closure Yeah this is from SparkUI, whenever you just tap on the add bar button item, this will be executed If you are not a fan of SparkUI, if you just want to build it out in UIKit just go ahead and use an Objective C func there selector and yeah you will be just fine So let’s tap on their ad and as you can see we can add a task we can add a title here and if we tap or right over here we will be asked to choose a photo, we will not do that. We will do that later on and we can select the difficulty of our task and finally we can hit save there, but of course it’s not doing anything just right now So yeah and we can just go back So let’s add our task to our managed context So let’s go into our ad view controller and let’s find this save bar bottom item As you can see it’s already fetching the title and the difficulty so it’s guard let unwrapping and selecting our difficulty from our difficulty picker So now it is time to save the task So what we are going to do is create an extension here which will basically do all the core data stuff. So we don’t clutter all that function. So there we go, we have a safe task. I already created this with the title difficulty and so on So let’s just create our task So first of all let’s create an entity. So if you recall the previous video we created an entity and then a task That’s where I had that strange crash First of all an entity, then a task with that entity So that entity is an optional with guard let unwrapped that last time but yeah it could be okay but let’s just, so no we force unwrapped it with the band Whoa oh, crazy. We need to guard unwrapped it Okay! So guard unwrap this Let and entity, there we go Entity and this will be an NSEntityDescription and NSEntityDescription and make sure you imported CoreData if you already haven’t done so in your project Entity for a name and that will be task and in the managed object, managed context sorry and that is why it’s important that it’s the same managed context and we just imported it like we just inherited it Okay! Otherwise we are just going to return, there we go. Now that we have our entity let’s create our task and that will be a task and now we are not going to use a managed object we are going to create an actual task and because we do have that class created we can do so and we are going to select entity here, so there we go and managed managed context. So if you are wondering how all of this like when we are saving how does the manage context know what to save well this is where we insert our task into the managed context So this is the link Okay! So now it’s pretty simple Just go on to task and add whatever you want. So the title will be our title here. Let’s move forward Our task should also have a difficulty So difficulty equals and yeah this needs to be an Int64, so I’m already converting it to Int64 from the difficulty property that we have on this function. Next up is ID, image data Task image data and that will be the images. So the UIImage that we are grabbing here PNG data. Now this image is on the image view. I won’t go into how to select an image, it’s already in the project and it’s using SparkUI but yeah you need to

grab an image. A UIImage, you need to have an image available and we are going to use the PNG data of that image Next up is the ID, task ID and that will be a simple ID and as you can see I’m setting a default UUID here because yeah why not and on the created at we hava a setting a date, now so this is now currently when we are creating and on completed I’m setting it to false by default because created tasks should be false on the completed by default So next up comes created at, so created at equals created at which is now and task dot completed equals completed, okay Now here comes the color. So we don’t have any color here but we are going to select a specific color on the difficulty given because if you recall on the previous video we just had that random, randomly we just created random colors for those cells Now we want to have it as a difficulty So that will be a switch, switch not swift, switch, there we go On the difficulty and because this is an integer we want to have a default value. So default and that will just simply return If we don’t have a valid difficulty we just simply return, we’re not going to do anything and let’s add some cases So case one and then the task dot color should be UIColor dot and let’s say system red because if the difficulty is one that’s the hardest one So that should be it and let me just copy this out and paste a few more here and let’s just move this out of the way and select all of that hit ctrl I for indentation, two, three, four and five and here we should have system orange Here is yellow, here teal and blue Alrighty and finally we want to save that context and again we just go, I’m telling you again because we did this already in the previous episode Manage context dot save and this will throw an error and we are not going to have a pattern. We are just going to use a completion. So completion, there we go dot failure dot error. So we are just completing because as you can see we do have a result on the completion handler with an error and of course we need to have a success value also So completion dot success equals true. Really, really nice So we are going to use this one on the save button. So let’s scroll all the way up and now that we have the title and the difficulty we are just going to call simply self dot save task and there we go. Se are just going to use the shorter version because yeah default values are okay and let’s add in here title difficulty and our image, there we go So yeah we just guard let unwrap all of those so we are ready to go and result comes here and let’s just do the switch statement. Switch result and let’s just wait for xcode to complete this So now here we have finished So if the save finished successfully So finished and let error So in the error we are going to use, we’re just going to show an alert show error. This comes from SparkUI So you could just simply show a simple UIAlert, if finished else and if not finished then something went wrong, we don’t even return a false value but let’s just handle all of that with a show error something went wrong. So we are going to say something went wrong. Otherwise we are

just simply going to pop the view controller So everything is saved, so self navigation controller pop view controller animated true, okay. Let’s just build and run and see if all of this works and I can see that this is coming a really, really long tutorial so I will chunk it into two We will meet from here in the next video So let’s add a title here like do yoga Let’s select a photo Let’s choose a photo because we are inside the simulator Whoa my camera battery died on me So that is a good sign that I need to stop right now because it’s taking while I believe it’s more than half an hour Now so I will chunk this tutorial into two and in the next video we are going to talk about how to fetch all of this data from CoreData So I will see you in there Because my camera battery died, I have decided to cut this tutorial into two parts. So in this first part you have learned about attributes and how to save data and in the next one we are going to talk about how to fetch that data into that to-do list So make sure to check out that one and in the meantime while you wait for that make sure to check out these videos as well too I talk a lot about UIKit, Swift in general and as usual I will see you in the next one

You Want To Have Your Favorite Car?

We have a big list of modern & classic cars in both used and new categories.