Working with the local storage can be a little overwhelming if you are not following the right design pattern be it core data or any third party tool, in this tutorial I explain the following topics 1. Why singleton approach is not good for local storage along with technical reasons 2. Introduction to repository pattern 3. How to avoid breaking abstraction 4. Some fun with Generics in the persistent Storage class And some of my examples as usual This is the first part and the second part will be out this week, am not going to keep you waiting for an entire week for this. Please do like this video and do subscribe to the channel and share it with your iOS group
Generally, I don't comment on videos, but today taking time to comment. because it is worth watching and thumbs up. No words to say. Actually, I am an iOS Trainer but I learned something new from you. Thanks
This is premium quality content for which people pay and watch and you are just giving it for free and explaining in very simple term. Thank you for sharing this Ravi.
Bro, I am really thankful that you are here to help developers like me , whenever you say “Creative Developer” I tend to creat a creative image of mine in my mind 😅 where I see myself doing creative things 😐
You see today your code may not be that good and don’t feel bad about it, practice it, always think before you code, read more on solid principles. Am sure you will write code 100 times better than me. It took me time to practice, implement, think like this and it was a bumpy ride of trial and errors. There’s a story behind how i started to code like this or whats my journey in programming i will share it someday on my channel
Code Cat I will be waiting for that , I am sure it will definitely help me , you lectures are so amazing that when the tutorial completes I actually say to myself arey khatam why it’s not more 😅 I literally get so engaged with the content ❤️❤️❤️
@@AnuragKashyap17 Second part iska mai Wednesday tak release karne ka try karta hu and those are kind words thanks man it means a lot to me. I think ye 1000 subscriber special video banata hu mai.
Thanks yaar, socha nahi tha aisa kabhi hoga coz maine assume kiya tha ki 200 ya 150 hoga but I will surely share a thank you video and my journey in programming.
Thanx Ravi, I wish I could give you more likes for the video 😊. Loved the way you organised the code. The channel deserves more subscribers for the quality of contents you are providing. Looking forward for next video of the series. 👍
Thanks Vikas that’s really nice of you to say such kind words. Please feel free to ask questions on this topic and share this channel with your iOS group.
Sorry @Vikas I could not get back to you, I am not getting notifications for the comments so i manually check. So coming to your question if the parameter was a string or int then the conversion was not required but in the case of UUID since UUID does not conform the protocol CVarArg the manual conversion was needed. so when I did "Id as CVarArg" I am converting the UUID to a va_list which is variable argument list in C programming. Let me know if that answers your question and once again apologies for not getting back in time.
Hey Vinod long time no see, yes i did share my channel yesterday with Arjun, hope it helps you in understanding the core concepts of swift and thank you for your kind words. Do share it with other members as well and do ask them to subscribe
hiii ravi .... i am became ur fan..... u r too too gud.... u r talking both in hindi and english inbetween thats y I understand Language is the problem for me . I dont know hindi. Its my kind request for you to do explain in English.. It will be very helpful for many...
Hey Sharmi, I would like to apologize if language has caused you issues in understanding the topic, I will be very much happy to answer your queries on this topic so please do feel free to ask questions. I think it's almost time to have a poll on this, you see I would be happy to make a video in English but mostly the viewers prefer hindi, but from time to time I keep on doing a temperature check to ask my viewers what they want and soon I will add a poll for this on my instagram handle :)
I really like background music and motivation thought on wallpaper . I learn so many thing from this video. very simple and easy to understand . I think you are big fan of Einstein.
Thanks a lot for the comment, I am just a curious person who wants to explore and have fun with technology so I keep on moving from one technology do couple of projects in that and then move to the next one. My Dad, Einstein, Nikola Tesla, Bruce Lee and Steve Jobs are my greatest inspirations of all time. Writing clean code is very easy and that's all I want to show from this channel.
@@CodeCat15 Sir you follow these genius and programming and I follow your your RUclips it will clear my all concept surely due to explanation in simple and clear with real life example . In IT world specially in India Nobody guide to junior developer properly they think if they guide to junior and if junior achieve excellence and then their job or position will be in bad situation , means they used politics agenda in company
@@CodeCat15 With the help of your channel lots of junior or mid level developer will be understand what the things is going on in iOS and difference between quality code and unwanted and hard to manage code.
Nobody guided me too so I know the pain and I know what you might be going through, I am not saying its an easy path but all we should focus on is improving our self as a programmer. Not all seniors are like that some are very kind and try to help the junior devs as much as they can but its very unfortunate that you have to come across such creative seniors. I think if we just do our job and make sure we cover all the use cases then people no matter how bad they play politics won't be successful as our work speaks for itself. This is my advice to you, just focus on your work, make sure all the use cases are covered, my channel is very new so i may take time to cover some topics but ultimately I will cover them with easy examples.
Bro, Please do try to explain dependence inversion in this topic as well. I know you have already explained and there is video out there for this but please do explain the same here as well, like fetching the data from the api and then storing it to the database for which we will be having different class for Core data handling and API handling so I guess in that case Dependency Inversion can be demonstrated again, please correct me if I am wrong. That will actually be a real life scenario when coding. And will actually give a more clearer vision on both these topics.
the dependency inversion principles states that the high level modules do not depend on low level modules, so in this example, if you observe I have a protocol in the repository so all I am saying is yes it is one of my topics for the core data playlist I already thought about this because this scenario has its own complexity which we need to dealth with because you will have two repositories here APIRepository to fetch data from the server for employee and a database repository for local storage and fun fact APIRepository cannot and should not call DatabaseRepository because then you introduce coupling between them, will I use DI or something better let's find out when I release that topic.
Hi Ravi. Can you make a video on how to make a boilerplate for core data such that I can directly download the files and include them in my code? More like a boilerplate instead of "Employee", or "CDEmployee" entity, we can take any entity that our project contains. (Using Generics)
Sir. I'm facing a problem on 18:40 let predicate = NSPredicate(format: "id==%@", id as CVarArg) the like show me this error "Cannot convert value of type '(any AnyObject).Type' (aka '(any AnyObject).Type') to type 'any CVarArg' in coercion | Insert ' as! any CVarArg'
Hey Ahsan, welcome to the channel thanks a lot for your comment, please feel free to ask questions on this topic will be happy to answer them and do share this with your iOS group on WhatsApp or Facebook
In the first part of core data I am explaining why I created this singleton class and what was the though process behind it. Do check the introduction to core data video given below is the link for the same Link: ruclips.net/video/wAy3S1MnKYY/видео.html
Hi Sir. Why do we need convertToEmployee method when the data we are getting from fetchManagedObjects method is already an array of the type we declared? func getAll() -> [Employee] { guard let employees = PersistentStorage.shared.fetchManagedObject(managedObject: Employee.self) else { return [] } return employees } Is this code not enough on getAll method?
You could do that but then it will expose the domain model to the client layer which is not advisable because the client might not need all of those OR before passing to the client there could be certain data-formatting that you may need to do like formatting the date based on the user's locale OR formatting a string to remove special character and hence you should always maintain a separation between domain and the client data model
Here are the drawbacks of using a generic singleton class for CRUD operations 1. Lack of separation of concern: Generic singletons mix data access logic with application logic, violating the principle of separation of concerns and making code harder to maintain. 2. Testing Challenges: Tight coupling to data sources in a singleton makes unit testing difficult, as you can't easily substitute mock data for testing purposes. 3. Limited Adaptability: Generic singletons lack the flexibility needed to adapt to changing data access requirements, potentially causing difficulties when switching data sources or optimizing data access. 4. Scalability Challenges: As your codebase becomes more complex and your data access needs become more specialized, maintaining a single monolithic class for all CRUD operations can become unwieldy. The repository pattern allows you to create specialized repositories for different entities or data types, promoting a more scalable and maintainable architecture. while using a generic singleton class for CRUD operations may seem like a straightforward solution initially, it can lead to several drawbacks, including poor separation of concerns, reduced testability, limited flexibility, and scalability challenges.
We don’t want that class to be inherited by other class and hence to make sure that does not happen we mark it as final If not then someone will get an idea of subclassing it and now you have two classes with same behavior manipulating the core data store that’s violation of single responsibility principle
Hello Saqib, welcome to the channel image ko aap as data store kar sakte ho, so you do have data as a type in core data, to using that you can store binary, there are other ways too of doing that maine part 2 mai usse explain kiya hai. Is video mai agar aapko koi doubt ho to please puchna zaroor aur ye channel aapne iOS group mai bhi share karna
You need to have the entities defined in the xcdata model with their relationships and then you can just inject data as shown in the demo. Whatever the case you need to have all your entities with their relationships defined in the model before you write the code, if you try to do things dynamically then it may cause issues or worst versioning can be a pain to deal with. Let me know if that answers your question and if not send me an email with a sample codebase of what you are trying to accomplish with what have you tried so far
The employee record will be coming from the UI. User will enter the record and it will be saved using core data, did I answer your question? If not then please feel free to ask I will be happy to answer them. Also there's a part 2 of this video I recommend seeing that as well
@@CodeCat15 Have you made any struct named Employee cause the NSManagedObject is CDEmployee right and in the protocol you are declaring Employee. Thats what he is asking. Whether we have to make a struct which has same as CDEmployee
Working with the local storage can be a little overwhelming if you are not following the right design pattern be it core data or any third party tool, in this tutorial I explain the following topics
1. Why singleton approach is not good for local storage along with technical reasons
2. Introduction to repository pattern
3. How to avoid breaking abstraction
4. Some fun with Generics in the persistent Storage class
And some of my examples as usual
This is the first part and the second part will be out this week, am not going to keep you waiting for an entire week for this. Please do like this video and do subscribe to the channel and share it with your iOS group
Great video
Just one suggestion
Instead of using forEach loop to convert cdEmployee to employee, you can use map function
Generally, I don't comment on videos, but today taking time to comment. because it is worth watching and thumbs up. No words to say. Actually, I am an iOS Trainer but I learned something new from you.
Thanks
This is premium quality content for which people pay and watch and you are just giving it for free and explaining in very simple term. Thank you for sharing this Ravi.
Glad you enjoy it!
Bro, I am really thankful that you are here to help developers like me , whenever you say “Creative Developer” I tend to creat a creative image of mine in my mind 😅 where I see myself doing creative things 😐
You see today your code may not be that good and don’t feel bad about it, practice it, always think before you code, read more on solid principles. Am sure you will write code 100 times better than me.
It took me time to practice, implement, think like this and it was a bumpy ride of trial and errors.
There’s a story behind how i started to code like this or whats my journey in programming i will share it someday on my channel
Code Cat I will be waiting for that , I am sure it will definitely help me , you lectures are so amazing that when the tutorial completes I actually say to myself arey khatam why it’s not more 😅 I literally get so engaged with the content ❤️❤️❤️
@@AnuragKashyap17 Second part iska mai Wednesday tak release karne ka try karta hu and those are kind words thanks man it means a lot to me. I think ye 1000 subscriber special video banata hu mai.
Code Cat Arey wahhh!!!! Congratulations in advance ❤️❤️❤️ You deserve a ton of subscribers bro 🤘🏽
Thanks yaar, socha nahi tha aisa kabhi hoga coz maine assume kiya tha ki 200 ya 150 hoga but I will surely share a thank you video and my journey in programming.
Thanx Ravi, I wish I could give you more likes for the video 😊. Loved the way you organised the code. The channel deserves more subscribers for the quality of contents you are providing. Looking forward for next video of the series. 👍
Thanks Vikas that’s really nice of you to say such kind words. Please feel free to ask questions on this topic and share this channel with your iOS group.
@@CodeCat15 Could you please explain id as CVarArg part of the predicate. When should we caste any property as CVarArg ?
Sorry @Vikas I could not get back to you, I am not getting notifications for the comments so i manually check. So coming to your question if the parameter was a string or int then the conversion was not required but in the case of UUID since UUID does not conform the protocol CVarArg the manual conversion was needed. so when I did "Id as CVarArg" I am converting the UUID to a va_list which is variable argument list in C programming. Let me know if that answers your question and once again apologies for not getting back in time.
Hi Ravi Sir, This is Vinod Mahale. (iPhone batch no 17, Quagnatia..) Glad to see you here. Got your channel link from Arjun...
Hey Vinod long time no see, yes i did share my channel yesterday with Arjun, hope it helps you in understanding the core concepts of swift and thank you for your kind words. Do share it with other members as well and do ask them to subscribe
@@CodeCat15 Sure sir
hiii ravi .... i am became ur fan..... u r too too gud.... u r talking both in hindi and english inbetween thats y I understand Language is the problem for me . I dont know hindi. Its my kind request for you to do explain in English.. It will be very helpful for many...
Hey Sharmi, I would like to apologize if language has caused you issues in understanding the topic, I will be very much happy to answer your queries on this topic so please do feel free to ask questions.
I think it's almost time to have a poll on this, you see I would be happy to make a video in English but mostly the viewers prefer hindi, but from time to time I keep on doing a temperature check to ask my viewers what they want and soon I will add a poll for this on my instagram handle :)
@@CodeCat15 k bro ...
I really like background music and motivation thought on wallpaper . I learn so many thing from this video. very simple and easy to understand . I think you are big fan of Einstein.
Thanks a lot for the comment, I am just a curious person who wants to explore and have fun with technology so I keep on moving from one technology do couple of projects in that and then move to the next one.
My Dad, Einstein, Nikola Tesla, Bruce Lee and Steve Jobs are my greatest inspirations of all time. Writing clean code is very easy and that's all I want to show from this channel.
@@CodeCat15 Sir you follow these genius and programming and I follow your your RUclips it will clear my all concept surely due to explanation in simple and clear with real life example . In IT world specially in India Nobody guide to junior developer properly they think if they guide to junior and if junior achieve excellence and then their job or position will be in bad situation , means they used politics agenda in company
@@CodeCat15 With the help of your channel lots of junior or mid level developer will be understand what the things is going on in iOS and difference between quality code and unwanted and hard to manage code.
Nobody guided me too so I know the pain and I know what you might be going through, I am not saying its an easy path but all we should focus on is improving our self as a programmer.
Not all seniors are like that some are very kind and try to help the junior devs as much as they can but its very unfortunate that you have to come across such creative seniors. I think if we just do our job and make sure we cover all the use cases then people no matter how bad they play politics won't be successful as our work speaks for itself.
This is my advice to you, just focus on your work, make sure all the use cases are covered, my channel is very new so i may take time to cover some topics but ultimately I will cover them with easy examples.
@@CodeCat15 Sir, Thanks for your advice
Hi, In EmployeeDataRepository swift file in protocol Employee comes from where??
Bro, Please do try to explain dependence inversion in this topic as well. I know you have already explained and there is video out there for this but please do explain the same here as well, like fetching the data from the api and then storing it to the database for which we will be having different class for Core data handling and API handling so I guess in that case Dependency Inversion can be demonstrated again, please correct me if I am wrong. That will actually be a real life scenario when coding. And will actually give a more clearer vision on both these topics.
the dependency inversion principles states that the high level modules do not depend on low level modules, so in this example, if you observe I have a protocol in the repository so all I am saying is yes it is one of my topics for the core data playlist I already thought about this because this scenario has its own complexity which we need to dealth with because you will have two repositories here APIRepository to fetch data from the server for employee and a database repository for local storage and fun fact APIRepository cannot and should not call DatabaseRepository because then you introduce coupling between them, will I use DI or something better let's find out when I release that topic.
Code Cat cool enough 🤘🏽🤘🏽🤘🏽
Thank you!, why you have not added this operations in earlier CoreDataDemo Project??, that will be very helpful to understand.
Thank you so much for this..
Am glad this was helpful @Shahbaz. Please feel free to ask questions on this topic will be happy to answer them
Thank you. At time 15:11 what shortcut you used at line 41?
its not a short cut but default suggestion from xcode itself I just pressed enter that's all
@@CodeCat15 Okay 👍
Sir, ye core data ke Operations kis thread pr hote hain (main or background) if we do not specify any explicitly ?
Hi Ravi. Can you make a video on how to make a boilerplate for core data such that I can directly download the files and include them in my code? More like a boilerplate instead of "Employee", or "CDEmployee" entity, we can take any entity that our project contains. (Using Generics)
good one brother
Welcome to the channel Vashu, am glad this video was helpful. Please feel free to ask questions on this topic and so share it with your iOS group 😊
I want to know how to fetch records with multiple parameters, like fetch employee data based on empID, empSalary, empDpt, etc.?
By using NSCompoundPredicate
Sir. I'm facing a problem on 18:40
let predicate = NSPredicate(format: "id==%@", id as CVarArg)
the like show me this error "Cannot convert value of type '(any AnyObject).Type' (aka '(any AnyObject).Type') to type 'any CVarArg' in coercion | Insert ' as! any CVarArg'
if you do the force unwrapping then does it work?
BEST ONE 👍🏻
Hey Ahsan, welcome to the channel thanks a lot for your comment, please feel free to ask questions on this topic will be happy to answer them and do share this with your iOS group on WhatsApp or Facebook
Sure
very helpful!
from where did u get this persistantstorage object can we handle persistant storage object in base data repository globally ?
In the first part of core data I am explaining why I created this singleton class and what was the though process behind it. Do check the introduction to core data video given below is the link for the same
Link: ruclips.net/video/wAy3S1MnKYY/видео.html
Hi, Can you make one video for which store data from API response with the relationship if possible?
I am going to, actually there are 2 maybe 3 videos which I want to explore with core data and the topic you requested Dipak is one of them.
Hello,
i have save four textfiled data, but when i fetch and show on tableview it shows only one entry (last textfield data), any suggetion?
Seems like a data binding issues, make sure all those values are set to the attribute of the entity.
ye CVArg kya hia ? Please explain these things while creating the videos thansk
Can we use higher order function for filter ?
Yes you can at it works well too :)
We really have a similar mind Code Cat.
Are you my twin brother Nitesh hahaha cheers 🍻
Hi Sir. Why do we need convertToEmployee method when the data we are getting from fetchManagedObjects method is already an array of the type we declared?
func getAll() -> [Employee] {
guard let employees = PersistentStorage.shared.fetchManagedObject(managedObject: Employee.self) else { return [] }
return employees
}
Is this code not enough on getAll method?
You could do that but then it will expose the domain model to the client layer which is not advisable because the client might not need all of those OR before passing to the client there could be certain data-formatting that you may need to do like formatting the date based on the user's locale OR formatting a string to remove special character and hence you should always maintain a separation between domain and the client data model
Good
Without going for repository pattern why can't we use generic singleton class for CRUD operations
Here are the drawbacks of using a generic singleton class for CRUD operations
1. Lack of separation of concern: Generic singletons mix data access logic with application logic, violating the principle of separation of concerns and making code harder to maintain.
2. Testing Challenges: Tight coupling to data sources in a singleton makes unit testing difficult, as you can't easily substitute mock data for testing purposes.
3. Limited Adaptability: Generic singletons lack the flexibility needed to adapt to changing data access requirements, potentially causing difficulties when switching data sources or optimizing data access.
4. Scalability Challenges: As your codebase becomes more complex and your data access needs become more specialized, maintaining a single monolithic class for all CRUD operations can become unwieldy. The repository pattern allows you to create specialized repositories for different entities or data types, promoting a more scalable and maintainable architecture.
while using a generic singleton class for CRUD operations may seem like a straightforward solution initially, it can lead to several drawbacks, including poor separation of concerns, reduced testability, limited flexibility, and scalability challenges.
@@CodeCat15 Thanks ravi for the sharing disadvantages of using generic singleton pattern
Glad it was helpful, please feel free to ask more questions
can u please tell me when u will upload next part of the video
Will be done in this week mostly by Wednesday, let me know if you have any questions on this and please do share this with your iOS group
@@CodeCat15 hey waiting for video
Why do we declare singjeton class as final??
We don’t want that class to be inherited by other class and hence to make sure that does not happen we mark it as final
If not then someone will get an idea of subclassing it and now you have two classes with same behavior manipulating the core data store that’s violation of single responsibility principle
sir image ki primitve type kkyya h to .. image variable kaise bnne ga
Hello Saqib, welcome to the channel image ko aap as data store kar sakte ho, so you do have data as a type in core data, to using that you can store binary, there are other ways too of doing that maine part 2 mai usse explain kiya hai. Is video mai agar aapko koi doubt ho to please puchna zaroor aur ye channel aapne iOS group mai bhi share karna
Can you show us how to create that Models also? coordination is missing when trying the trial of this tutorial in Xcode .
I am assuming you mean the core data models? Steps to create them are shown at 10:04 let me know it that’s what you are looking for
@@CodeCat15 not there. It’s here 15:21. I’ve looked into your GitHub files for that. No problem. 🙂
Oh okay cool 👍 feel free to ask questions on this and do share with your iOS group if this video was helpful
@@CodeCat15 sure👍
Sir how to add multiple entities??
Multiple entities as in from an API?
@@CodeCat15 no no sir, you know we can create multiple entities and can make relationship between these entities
You need to have the entities defined in the xcdata model with their relationships and then you can just inject data as shown in the demo. Whatever the case you need to have all your entities with their relationships defined in the model before you write the code, if you try to do things dynamically then it may cause issues or worst versioning can be a pain to deal with. Let me know if that answers your question and if not send me an email with a sample codebase of what you are trying to accomplish with what have you tried so far
@@CodeCat15 ok sir..sure
Employee comes from where
The employee record will be coming from the UI. User will enter the record and it will be saved using core data, did I answer your question? If not then please feel free to ask I will be happy to answer them.
Also there's a part 2 of this video I recommend seeing that as well
@@CodeCat15 Have you made any struct named Employee cause the NSManagedObject is CDEmployee right and in the protocol you are declaring Employee. Thats what he is asking. Whether we have to make a struct which has same as CDEmployee
Too much Complicated Video, also at 18:30 why using NSPredicate, what is it? why using format: "id==%@" nothing emplained. Just coding is going on.
Sorry if you feel that way, NSPredicate is a class that works like a filter in core data it's similar to a WHERE clause in SQL, hope that helps.
It's incomplete video.
Checkout the core data playlist you’ll find the second part there.