Mohammad, I found that my "title" textField input was intermittently being truncated (last character). So, to fix this problem, I added @Published to the title variable in the TaskListViewModel. Some of your viewers may be experiencing the same issue. This fixes it. Many thanks for the video, this is watershed code that can be used over and over in many Core Data applications.
First of all, very nice and clear presentation style 👍 I like that you don’t do the actual core data stuff directly in your view model. What I don’t like though, is that you're putting everything inside this common CoreDataManager class. That might be because of this very basic example with only one entity, but if you want to teach a good architecture to beginners you should show directly something which is able to scale. If the app will grow with more views, view models and entities this CoreDataManager will get very big and messy soon. Other than that, I would recommend a reasonable architecture should separate the handling from the NSManagedObjectContext from it’s usage. You are also using the singleton pattern here in a way which I wouldn't recommend. For instance, there is no way to use a memory only persistence store if you want to write unit tests. What I would suggest: - Handle the NSManagedObjectContext in an own class similar to the one Xcode generates if you check CoreData while creating a new project. IMHO it’s okay to make this a singleton as long you use that only as the default parameter inside an init method. - Write separate classes for each entity (I call them Repositories) which abstract the CRUD operations and in which you inject the context. - Make these repositories each implement a protocol - The view model only knows the protocol of the repository With this architecture you can write tests for the repo with an memory only core data context and if you want to write tests also for the view model, you can replace the repo with a mock / dummy which doesn’t use core data at all.
Thanks for your comments. I am a believer is baby steps meaning taking small steps to a bigger solution and not overly complicating stuff from the beginning. In my last video, I put all the code in Views just to show how Core Data persistence work. In this video, I used MVVM pattern. As you mentioned our app is simple so a single CoreDataManager would be enough. As the app scale we can have functionality in the model which inherits from the BaseModel. I covered that in my Core Data course on Udemy. For Unit Tests, I would focus on domain level tests and then tests everything else using integration and acceptance tests.
Thank you so much for this video. It helped me a lot to build an application for a job application. I just bought your course at Udemy! And I’m really interested to continue learning more about Core Data with you. I was thinking if you can create some videos building an app using Core Data plus API calls using MVVM on SwiftUI. Maybe explain as well about the folders created on MVVM architecture. Cheers
Thank you Mohammad Azam for this video. It explains a lot but unfortunately you don't provide all CRUD operations (for example, this video is missing how to edit existing tasks)
@@azamsharp I have bought both your Udemy courses: "MVVM Design Pattern Using Swift in iOS" and "MVVM Design Pattern in SwiftUI", what the main difference between them? I love this approach and thank to MVVM + CoreData I have started to understand how to use Core Data also. When you will create a new video, please don't forget to show the moment when it's best to initialise the Core Data, for example, if the app will use the pre-populated data (for example, a list of English words with translation, or a list of available addresses, for a situation when the app works offline without access to internet, without loading data from any remote database, in some Udemy trainings you provides examples of projects which uses Firestore db from Firebase, but if to create the app which doesn't use any remote databases?)
Hello Mohammad, thanks for the video. It's very helpful. I have a question though. Why do we have a deleteTask function in our view? Is it possible to move it anywhere else if we want our view cleaner, without any functions and such?
deleteTask inside the View is not doing much. It is invoking the delete method on VM. PS: My views on using MVVM has evolved and I no longer recommend MVVM architecture for SwiftUI applications. I have written about it extensively on my website. www.azamsharp.com
I need to do a Crud inside a Crud and pass the ID of the selected row, how do you recommend me to do that? is there anyway you could help me with that? if so how could I contact you
Nice video. I wouldn't mind purchasing the UDEMY class, but, it's not showing the "introductory" cost. I'm not going to spend close to $100 for my "hobby". Do you have a link for the cost shown on this video, or, a discount code to get it at that price? Thanks!
Hi Azam! love love your videos! I learn so much from you. I want to ask you one thing about this video, I usually use core data with fetchrequest property wrapper and I saw it nowhere on this video, how can I sort the data with this technique? i have created one with a date its what I'm gonna use to sort the data on the foreach loop and now I just can't seem to figure it out how can I sort it without @fetchrequest
Great video, thank you. I have a question - in contrast to the `persistence.swift` file template Xcode generates for a SwiftUI CoreData SwiftUI life cycle app, your `CoreDataManager` is a class, instead of a struct. Is there a particular reason for this? I want to learn how to apply your solutions but in a template generated initially by Xcode. Is it fair to think of both those files as performing the same thing / role in the app? Thanks again for the video. We really need tutorials on SwiftUI+CoreData+MVVM and this is very helpful.
@@azamsharp It's very nice to see such a quick response. : ) Can you clarify: Is your CoreDataManager file fulfilling pretty much the same role in your code as PersistenceController is fulfilling in the Xcode template? If I'm starting with the template, would you recommend editing the Persistence.swift file to be like your CoreDataManager, or is it a better idea to create a new file? I wonder because they both have the Singleton and that is what is confusing me some. These lines: static let shared = PersistenceController() let container: NSPersistentContainer
Hello dear author i have bought MVVM Design Pattern in iOS Using SwiftUI course but cant strart learn it because i have lattest version of xcode (and i havent files like AppDelegate and SceneDelegate) thaths why i am getiing a lot of errors in case, Can you rewrite lessons for lattest stable version of xcode. or say how can i get filw structure like your in my project thx
Thank you very much! This video solving the notion of viewmodel for me.
Mohammad, I found that my "title" textField input was intermittently being truncated (last character). So, to fix this problem, I added @Published to the title variable in the TaskListViewModel. Some of your viewers may be experiencing the same issue. This fixes it. Many thanks for the video, this is watershed code that can be used over and over in many Core Data applications.
First of all, very nice and clear presentation style 👍
I like that you don’t do the actual core data stuff directly in your view model. What I don’t like though, is that you're putting everything inside this common CoreDataManager class. That might be because of this very basic example with only one entity, but if you want to teach a good architecture to beginners you should show directly something which is able to scale. If the app will grow with more views, view models and entities this CoreDataManager will get very big and messy soon. Other than that, I would recommend a reasonable architecture should separate the handling from the NSManagedObjectContext from it’s usage. You are also using the singleton pattern here in a way which I wouldn't recommend. For instance, there is no way to use a memory only persistence store if you want to write unit tests.
What I would suggest:
- Handle the NSManagedObjectContext in an own class similar to the one Xcode generates if you check CoreData while creating a new project. IMHO it’s okay to make this a singleton as long you use that only as the default parameter inside an init method.
- Write separate classes for each entity (I call them Repositories) which abstract the CRUD operations and in which you inject the context.
- Make these repositories each implement a protocol
- The view model only knows the protocol of the repository
With this architecture you can write tests for the repo with an memory only core data context and if you want to write tests also for the view model, you can replace the repo with a mock / dummy which doesn’t use core data at all.
Thanks for your comments. I am a believer is baby steps meaning taking small steps to a bigger solution and not overly complicating stuff from the beginning. In my last video, I put all the code in Views just to show how Core Data persistence work. In this video, I used MVVM pattern. As you mentioned our app is simple so a single CoreDataManager would be enough. As the app scale we can have functionality in the model which inherits from the BaseModel. I covered that in my Core Data course on Udemy.
For Unit Tests, I would focus on domain level tests and then tests everything else using integration and acceptance tests.
is there a repo you could share that i could read to learn from with the implementation you are recommending
Thank you so much for this video. It helped me a lot to build an application for a job application. I just bought your course at Udemy! And I’m really interested to continue learning more about Core Data with you. I was thinking if you can create some videos building an app using Core Data plus API calls using MVVM on SwiftUI. Maybe explain as well about the folders created on MVVM architecture. Cheers
Very helpful Azam. thank you :)
so amazing sir, thank you so much
Great Video, ohne question, how do you this automatic insert Hstack?
Thank you
Thank you Mohammad Azam for this video. It explains a lot but unfortunately you don't provide all CRUD operations (for example, this video is missing how to edit existing tasks)
I may have some other videos on update functionality.
@@azamsharp Thank you! It will be nice to see.
@@azamsharp I have bought both your Udemy courses: "MVVM Design Pattern Using Swift in iOS" and "MVVM Design Pattern in SwiftUI", what the main difference between them? I love this approach and thank to MVVM + CoreData I have started to understand how to use Core Data also.
When you will create a new video, please don't forget to show the moment when it's best to initialise the Core Data, for example, if the app will use the pre-populated data (for example, a list of English words with translation, or a list of available addresses, for a situation when the app works offline without access to internet, without loading data from any remote database, in some Udemy trainings you provides examples of projects which uses Firestore db from Firebase, but if to create the app which doesn't use any remote databases?)
Hello Mohammad, thanks for the video. It's very helpful. I have a question though. Why do we have a deleteTask function in our view? Is it possible to move it anywhere else if we want our view cleaner, without any functions and such?
deleteTask inside the View is not doing much. It is invoking the delete method on VM.
PS: My views on using MVVM has evolved and I no longer recommend MVVM architecture for SwiftUI applications. I have written about it extensively on my website. www.azamsharp.com
@@azamsharp Thanks for the answer! I will surely check out the article!
Thank you Azam for this powerful video!
A trick : watch movies at kaldrostream. I've been using them for watching lots of of movies recently.
@Kristopher Joaquin Definitely, have been watching on kaldrostream for since december myself =)
@Kristopher Joaquin yup, been watching on KaldroStream for years myself :)
Thank you, that was amazing
I need to do a Crud inside a Crud and pass the ID of the selected row, how do you recommend me to do that? is there anyway you could help me with that? if so how could I contact you
Nice
How to delete all rows in one time? Please help^^ thank you
Nice video. I wouldn't mind purchasing the UDEMY class, but, it's not showing the "introductory" cost. I'm not going to spend close to $100 for my "hobby". Do you have a link for the cost shown on this video, or, a discount code to get it at that price? Thanks!
Use this: www.udemy.com/course/core-data-in-ios/?couponCode=5991B32AA46122F868DF
Hi Azam! love love your videos! I learn so much from you. I want to ask you one thing about this video, I usually use core data with fetchrequest property wrapper and I saw it nowhere on this video, how can I sort the data with this technique? i have created one with a date its what I'm gonna use to sort the data on the foreach loop and now I just can't seem to figure it out how can I sort it without @fetchrequest
Check if it has sortDescriptors property. It it does not then use the old way.
why was the update function not shown in the video?
Here is the code for update: github.com/azamsharp/MoviesAppUpdate
@@azamsharp Thank you very much for your fast reply. This is exactly what I missed in your Tutorial „Core Data …“ in Udemy
not able to see CoreData in iOS course in udemy business
Maybe Udemy will add it in the future.
Great video, thank you. I have a question - in contrast to the `persistence.swift` file template Xcode generates for a SwiftUI CoreData SwiftUI life cycle app, your `CoreDataManager` is a class, instead of a struct. Is there a particular reason for this? I want to learn how to apply your solutions but in a template generated initially by Xcode. Is it fair to think of both those files as performing the same thing / role in the app? Thanks again for the video. We really need tutorials on SwiftUI+CoreData+MVVM and this is very helpful.
Struct is mainly used for value types but CoreDataManager is not really holding any values. It is more acting like a service hence Class.
@@azamsharp It's very nice to see such a quick response. : ) Can you clarify: Is your CoreDataManager file fulfilling pretty much the same role in your code as PersistenceController is fulfilling in the Xcode template? If I'm starting with the template, would you recommend editing the Persistence.swift file to be like your CoreDataManager, or is it a better idea to create a new file? I wonder because they both have the Singleton and that is what is confusing me some. These lines:
static let shared = PersistenceController()
let container: NSPersistentContainer
@@rillieux1792 yes it is the same role
Could you upload how to update a task?
Just change the values and perform save on the model object
👍
Hello dear author i have bought MVVM Design Pattern in iOS Using SwiftUI course but cant strart learn it because i have lattest version of xcode (and i havent files like AppDelegate and SceneDelegate) thaths why i am getiing a lot of errors in case, Can you rewrite lessons for lattest stable version of xcode. or say how can i get filw structure like your in my project thx
What errors are you facing?