Introduction to Core data Swift 5 iOS Hindi tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome to another episode of coding with codecat this is ravi in today's session, we are going to explore core data, the code done in today's session is available in github and google drive and if you need the code what you can do is check the video description click on the link and download the code for you before we begin with this session, if you like the work that I am doing if you are learning something from my videos, then please do subscribe to the channel and share it with your iOS group that being said lets begin our today's session in most of the cases, mobile app has to be connected with the internet to request data from the server and to receive a response back and even for push notifications but a user cannot always be connected with the internet so when a user mobile is not connected with the internet, few mobile apps show an alert which is no internet connection it basically is an indication to the user that you don't have an internet connection the app won't work but there are some apps who allow the user to do basic functionality without the internet take WhatsApp for example if you turn off your mobile data or wifi even then too you can send message to your friends or in groups the only difference is that your friend will not get those messages because your mobile has no internet but when your mobile is connected with the internet the immediately all those messages are delivered to your friends so we can say that when your mobile was not connected to the internet then all those messages which you sent to your friend WhatsApp might have stored all those messages in a local storage and once it detected the internet, it immediately sends all those messages this technique is called as offline data storage or offline mode which simply means if you don't have the internet save the data in the mobile local storage and once you detect the internet then push all those saved data to the server this is just one scenario, we have a lot more where you can use local storage and allow the user to perform basic operations without internet some application like to-do list or some workout tracker they just rely on local storage and such applications don't need to send their data on the server so such apps don't really care about if they are connected to the internet let's focus on the word local storage depending upon your application you store a specific amount of data which later you may share it with the server or maybe not, and it completely depends on what your application decides to do with that data now it's true that you have to save data so that when the time is right you can make use of that data but how will you save this data? and what will you use to save this data? even before you save the data, it's important to understand what is the category of that data? because there are two categories small data set, which means locally I just want to store 2 - 4 values second is a large data set from the server, you are getting too many records and you have to store the entire record from the server there are couple of ways in iOS which you can use to store data locally like property list, user default, SQLite, core data and keychain the approach that you choose from this list should depend on the category of your data let's say from the server you are getting a single user id and every time you have to use this user id the for such small data set, UserDefault should be your first choice tools such as core data and sqlite should only be used in your application when you want to store large data set even for storing one or two values if you are using tools like core data or SQLite then you are actually increasing the complexity of your code remember in programming the goal is to make things simple and not complex that being said let's explore core data core data is a very vast topic and in the coming sessions, we will explore it in details one thing that I hear developers saying all the time core data is a database, well it's not a database core data is a framework as you can also see in apple's developer documentation there's a specific word that comes up when you read about core data which is object graph object graph is a topic which I will cover in a separate video Apple has created a very smart framework which has simplified the work of us developers to store data and not only that in the developer documentation apple has specifically mentioned if you use core data then it reduces 50-70% of your code because there are a lot of things which core data handle for you the one thing I really like about core data is how it manages relationships in the coming sessions when we learn relationships then I will clarify and speak more on the term object graph but for now, let's focus on the basics of core data let's begin the session with an example whenever you want to learn a new language let's say you want to learn Marathi then there are several rules of that language it has basics and a grammar that you need to follow and similarly when you learn a new technology or a framework that too has some specific rules and vocabulary and once we know what those rules are then we can play with them and do anything for today's session, I want to focus on these 3 classes of core data majority of the time our interaction is with these 3 classes there is one more class but let's talk about that in the next session let's begin with NSManagedObject class the first step for understanding NSManagedObject is to understand what's an entity in database, every record has a specific table like employee records will only be stored in employee table these tables are made up of entity in this case employee entity to be very specific but what really is this entity? the models that we make, in this case, we have an employe model in database language, these models are called as entity and since models have properties in this example, we have 3 properties for the employee which are name, id and address in database language, these properties are called as attributes just like in English we say food, in Hindi khana, and in Marathi jevan all of this means the same but due to languages we call it differently so let's go ahead and create our first entity in core data so let me switch to XCode I am going to create a single view app I will name it as core data demo and since we are using core data there's a small checkbox over here which says use core data, so I am just going to tick it next create the application which we created looks like a regular app but at the time of creating this application we checked to use core data checkbox we have a new file over here which says XCDataModel and in this file you will create entities, so let us quickly do that we are going to create the entity with the help of this button which is Add Entity and over here you can see we have the entity I will name it Employee and just now we have seen every entity has an attribute so we are going to create some attributes I will call it name and the datatype of name is going to be string for now, let's just go with one attribute on the right-hand side you have something called as the data model inspector over here you have a small property named codegen which stands for code generation which is set to class definition we will change it from class definition to manual and why we are doing this will be explained in few minutes we understood what an entity is now let's talk about NSManagedObject subclass as we know in swift, every class has a base class which is NSObject similarly, every entity in core data has a base class which is NSManagedObject which manages your entities all the entities used in core data it inherits from NSManagedObject normally while creating model, we just go to the project navigator -> Right-click and then created a new file but this is core data entity we will let the Xcode create the managedObject subclass for us and to do that you have to select editor you have to click on create NSManagedObject subclass over here you can see the entity we created which is employee the same is being reflected over here just make sure the checkbox over here is selected and by default Xcode selects the Xcode project group i am just going to select core data demo xcode has created two files for us one is employee core data class and the other is employee core data property let us explore the core data class first over here you can see Xcode has just created an empty class for us and this class inherits from NSManagedObject let us explore properties now in the case of properties you can see there is more code there's something called as NSFetch request which we will explore in few minutes over here we have our attribute which is name that we created the name here is it is having an attribute named NSManaged and over here you can see that the name is an optional string and this is optional because by default whenever you create an attribute xcode by default marks the attributes as optional when you create them and that's the reason why you are having this name attribute as an optional string lets talk about NSManagedObjectContext class for further reference I will call this class as context, so whenever I am saying context I am refering to NSManagedObjectContext class if you look closely at context class you have NSManagedObject over here and this is because in core data application whatever changes you do in your entities this context makes sure that all those changes are saved context manages all the entities of your class and not just one for creating the context, you don't have to write any code and the reason for that is when you select core data while creating the project, at that time Xcode creates some boilerplate code in the app delegate normally when you create an application you will never see something called as saved context or over here you have something called as the persistent container since we have selected core data, this is one of the reasons why you get some boilerplate code in the app delegate over here we have the persistent container which is having a property called as viewContext the interesting part of today's session is all the coding is done by Xcode and I have not written any code today persistent container class is responsible for creating and managing the core data stack before using core data, there's some basic management work that is done by the persistentContainer this persistent container creates the object of view context which we can access via property which you have already seen in this method so that was all about the 3 important classes in core data now finally its my turn to code if you have already used core data or if you have seen any video tutorial or blog one thing that you may have seen is everywhere they share the appDelegate reference to access this viewContext over here the best thing about programming is you can always improve your code and hence in today's session, we will use a better way than sharing the appDelegate reference we need the context which is present in the appDelegate and apart from context, in the appDelegate you have the application life cycle methods I don't want to touch the life cycle methods many developers share global variables inside the app delegate which I believe should not be done because there are better ways for sharing global variables I personally believe that if a class manages any life cycle events then do not touch it now let me explain this with the help of an example in hospitals, you have a ward named intensive care unit or ICU nobody can interfere with the machines in ICU some creative people may say that ICU is also a room go inside the room and you make touch the machines what will happen when a normal person does that? the person who will be on life support will die and that's the reason why admin of hospital does not allow anyone to enter the ICU because it's too risky you won't even see the machine used in ICU in any general ward I consider app delegate as the ICU of my application and that's the reason why I don't want to pass the reference of the app delegate to view controllers now the question is how can we do this without sharing the reference of app delegate so what i am going to do over here is, I will create a new file I am going to cut+paste the boilerplate code in app delegate I am going to get rid of the comments I will mark this class as final because both these contexts are referring to the same viewContext let me get rid of this where you shared the app delegate reference over here what I have done is I have created a final shared class which I will use in this class, 80% of the code is from the app delegate and I have just added that code in a shared final class the pattern I am using over here is a singleton pattern If you like this coding pattern where we are not sharing the app delegate we are using a shared final class then please do like this video and do subscribe to the channel so we have got an error so let us see what that is this is exactly the same code which you may have seen in so many blogs or video tutorial what's happening here is we are sharing the app delegate instance to access this particular function the reason why you are getting this error over here is because we have moved this savecontext function to our final class the problem with this code is, if I go and remove this save context I can easily access my life cycle functions and I think whatever class you have if it's not using a particular functionality then don't feed unnecessary function to that class and hence what we did is let me get rid of all of this this is more neat and clean because I am not feeding any unwanted functionality to my class I am only specifying the functionality that my class needs now let us move to view controller we are going to use the context over here which is present inside our persistent storage and since we have made changes to our entity and we know that since we made changes to the entity we have to save them so we will save the changes made to our entities by using the persistent storage save context function and now I will call this create employee inside my view did load so as you can see I am not able to tell if my record was saved or not so I am going to create one more function which is going to be I am going to inform my context that I am want to fetch some records and I want to fetch Employee records we have got an error over here so let us see what that error is so this particular piece of code can actually throw an exception so we will be encapsulating this inside a do..try catch so if you try to go ahead and use this result so this result is actually a collection of Any, but we don't want a collection of Any we want collection of employee so what we have to do is we have to convert this result which is a collection of Any to a collection of Employee and to do that I will be using the guard statement the result which gets captured here I will be printing it in the console so as you can see, in our console, Ravi is printed but I have one curiosity here where did core data actually save this record? and from where did core data fetch this record? there might be one more question from where did this fetch request come from so if you remember when we created the core data properties over here Xcode has created a function for us over here and we are using this function to fetch the employee records now let me answer where did core data save/fetch this record from and for that we need to write a small piece of code so let me run the application once again over here we have the document directory path so I will just go ahead and copy this inside the document folder, we have nothing as of now we are inside the document directory of our app and this is the spot where your iOS app will save application related files so over here documents is empty but if you go to the library you have something called as the application support and over here you can see there's a file called as coredatademo.sqlite we are going to open this file and for that we are going to use the DBBrowser for SQlite and I will go ahead and drag and drop this thing over here you can see you have a table named employee and this is the place where the record is saved and retrieved from by core data one more question would be how did this SQlite file get created? so for that let me stop the application and let me switch to the persistent storage when persistent container initializes the core data stack it creates a container with the same name as your project and the container which it creates is a SQlite file living inside the application support folder and let me remind you once again, the entire code over here is not written by me I just pasted it here from the app delegate and this is the beauty of core data which does so much for us internally without us having to write any code since now we have a basic idea about core data let me return back to the original question which was why did I set the codegen to manual/none from class definition let me do one thing, let me set it to class definition which is the default code generation style and what I am going to do is, I am going to delete these two files and I am going to build the application so as you can see we have got build succeeded but just now we have deleted those two files and my expectations was, since we deleted those files I should have got some errors in the view controller we are actually using this Employee class but as you can see this is not the case so let me right-click this and jump to definition and over here you can see now the statement is this file was automatically generated and should not be edited so let me see where this file is because as of now we don't have such file in our project so I will navigate show in finder so over here we have these two files but where are these files? so let me see where this guy is so it is buried very deep inside the derived data folder so whenever you select code generation class style what happens is the core data files, which are these two files core data class and the core data properties both of them gets saved in the derived data which is buried way deep inside a folder so I think a better way to do this is just keep it rather than class definition I would keep it to manual/none because this way I have complete control over the code I don't really like if I select class definition and xcode generates those files for me it might happen that I may land up to some issues so that's why for better control over my code I keep the code generation settings to manual/None rather than rather than the default which is the class definition this was an introduction session of core data I wanted to keep this session very basic by just showing some simple operations in the next video we will cover some more grounds with core data we will be learning operations like create, read, update and delete which are also called as CRUD operations along with writing clean code for your operations I know there will be too many questions with regards to what we learned today you may ask them via comments or you may email them to me my email id is codecat15@gmail.com If you like this video then please give a thumbs up and if you are new to the channel then, please subscribe that being said thank you so much for watching this have a nice day and happy iCoding bye bye :)
Info
Channel: Code Cat
Views: 13,814
Rating: undefined out of 5
Keywords: core data, core data hindi, core data in hindi, coredata in ios swift 5 hindi, how to use core data in swift 5, introduction to core data swift hindi, core data tutorial for beginners, core data tutorial for beginners hindi, core data for beginners, save data using core data swift, codecat15, codecat 15, core data tutorial swift 5 hindi, how to use core data hindi tutorial, how to use core data, core data swift 5, core data ios, core data swift, what is coredata
Id: wAy3S1MnKYY
Channel Id: undefined
Length: 22min 55sec (1375 seconds)
Published: Sat Jun 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.