when you create entity in core data model be sure after that to press cmd + s to save cuz Xcode mostly do not save file extension xcdatamodeld automatically
Hi Nick! I'm looking for a few adventures in the mobile world as a senior full-stack web developer and thanks to your courses I've been learning a lot! Already built my first simple app and I'm planning to tackle a second one soon. Thanks to your patience, awesome teaching skills, and great content, I feel like I have all I need to go into these waters, as I don't always understand Apple's documentations. Thank you very much for putting such an effort to this, enabling myself and many other people like me.
Though I learned to use CoreData somewhere else, your coding style is much more realistic that I can always learn something. That is why I keep watching your tutorials even on topics that I am familiar with. I am so thankful for your teachings of good qualities. Thank you!!
Glad I found your video - I was really struggling with Core Data in the MVVM pattern - I haven't found anywhere else that explains it so well - Thank you!
I come from a deep background in data warehouses, relational databases, and REST API testing with backend databases. I have worked with both SQL and no-SQL databases. In data warehouse development and testing, the ETL concept is key. The developer typically should create a software design specification with a source-to-target matrix such that any data transformations are addressed. The transformations or the "T" in "ETL" for extract, transform and load can get somewhat complicated. I have seen software defects rooted in transformation errors. So, the MVVM model is ideal in separating the architecture into components such that the ViewModel deals with transformations. So, the MVVM video is great. I appreciate Nick's attention to details in his presentation on how MVVM works. Now, the only area that gets little coverage in my opinion for multiple Core Data tutorials from either Nick or other influencers is the concept of many-to-many tables. In relational SQL, the SQLite database with many-to-many tables should have an ER implementation such that the many-to-many relation uses a hybrid intermediate table with primary keys from both tables. This allows the queries and inner joins that could be done. I was able to implement that easily using MySQL and SQLite with PyQt. I haven't seen any Core Data video that goes from the ER relation of a many-to-many relation with two tables and primary keys and discusses how that "intermediate table" with primary keys from both tables is addressed in Core Data. The Microolap Database Designer web page has this definition "The only way to create many-to-many (N:M) reference between two tables in MySQL is using an intermediate table, which records set association between primary keys of first table and primary keys of second table." I haven't seen a Core Data tutorial that addresses that in its many-to-many tables by going from the ER software design to the code implementation. Usually, the influencers just write code without ever addressing the written design of an ER model. Such development patterns are ripe for errors in my experience with testing data warehouses. I worked with Informatica developers and was trained by a guy with about 45 years of experience in ETL database development.
Update: I found additional resources that help me to understand what is going. The "many-to-many" relationship of table A to table B where an intermediate table C with primary keys from both A and B is replaced by a relation in the graph object model. I found a website that shows the checklist and a couple of other videos. So, I figured out how to make a traditional ER model then convert it to a graph object model which can then be implemented in SwiftUI Core Data. That ER to graph model conversion is usually missing in most Core Data videos. The typical computer science departments focuses on ER for training on databases. Given that graph object diagrams are largely untaught with comprehension, this can lead to design errors badly. The article I found is "RDBMS & Graphs: Relational vs. Graph Data Modeling".
I know you probably won’t see this , but thank you so much seriously. I didn’t know where I could learn SwiftUI and where to start but you help out so much . You deserve so much more subs
Thanks Nick! Simple and straightforward solution to the question of replacing @FetchRequest with ViewModel @ Published. It is unclear why Apple itself did not solve this issue, leaving the developers with a good, but primitive @FetchRequest
Oh sorry I didn't mean to miss that haha. It's pretty much the same logic that you can add to the FetchRequest. If you watch the next video, I believe we will cover sorting and filtering using CoreData with the MVVM approach.
YES, loved the video, help immensely, I also put this in the code: savedEntities = savedEntities.sorted(by: { $0.name ?? "missing name" < $1.name ?? "missing name" }) to sort .name, so I did learn something. Thanks again
Correct me if I'm wrong. If you were to reference the view model as Observable/stateObject in multiple views you would end up instantiating CoreData multiple times from each view. This behaviour doesn't seem optimal. Solutions may involve declaring it as a struct or passing it down as an environment object from the main view.
Hey bhagyash! You are correct! So in a real app, you could initialize the Core Data class when your app loads and then you would "inject" it into the view model by passing it into the View and then into the ViewModel initializer. However, I haven't covered Dependency Injection on my channel yet!
Hey, Nick. Not sure if you saw my previous question yet but if you did, I found a solution so please disregard. It appears as if replacing an existing project with a new project of the same name (using the "Replace" button), interferes with the indexing. Creating a completely new project with a new name fixed the problem. Thanks for everything you're doing. Your videos are an invaluable resource.
Thanks, Nick! I've been watching many videos on how to implement this, and it's been interesting to see the different styles. I'm guessing that if you had multiple vms that needed to listen to a persistentContainer, you'd put the container in some kind of manager, and then pass the context around to the several vm's using observableObject and @State to keep things in sync?
Hey David! That’s pretty close. I would create a class that has the persistentContainer and all the CoreData functions.. and then create a manager class. The manager class will hold the reference to the core data class. Then pass the manager to your VMs. The VMs call functions in the manager, which is essentially a funnel to call functions in the CoreData class. This creates an abstraction layer that will decouple the app from CoreData
Hi Nick, loving your videos. I'm using Xcode version 14.3, and for the life of me I cannot figure out how to get the colour literals to work the way shown in your videos. I cannot get the picker with all of the pencils in it to appear. Any tips?
Thanks for the vid.. Super helpful... Quick question though since it wasn't included in the video.. The delete func was super simple however should I need to delete all of the data, should I loop on the entity one by one and then delete? Is there any other easy ways to do it like container.viewContext.delete(.all)?
Hi Nick, thanks for this video! I have a question though. I want to implement Core Data in MVVM way as you do, but what confuses me is when I check the box Core Data when creating new project, there's a file Persistence.swift that has persistent controller, and I can't understand where it belongs. Is that ViewModel? Is it okay that it's struct and not class like you did with CoreDataViewModel? Is there a way I can keep this Xcode automatically created file but keep MVVM architecture like you have? Or should I start from scratch like you do in this video and not make Xcode create that Persistence.swift file?
I am no expert but every course I've taken on Swift every instructor has told us to never use those checkboxes for CoreData or Testing. Not sure if that is so we really learn how to implement those items or if the files created are so problematic in many cases? And I'm talking high-quality instructors like Sean Allen, Paul Hudson, Meng To of DesignCode, Ray Wenderlich's courses I've taken, Stewart Lynch, etc. So I am curious as well. ...
Hey Terry! The Persistance.swift file is just a convenience thing that Apple gives us. If you look at the code in Persistence.swift, almost all of it overlaps with the CoreDataViewModel that we built in this project. In production, we would probably separate the ViewModel logic from the CoreData logic. We do something like that in the next video in this series, where we create a CoreDataManager class, which would be the same as the Persistance.swift basically!
@@SwiftfulThinking Oh, thanks Nick! I didn't see next video, my bad. I see now you've created CoreDataManager and then made an instance of it in the ViewModel! Nice! Exactly what I wanted to know - how that persistence blends in with MVVM! Gonna keep watching that video, very interesting
@@ZeakZac Yes, exactly! That’s going to be another step towards a better approach. After that, instead of crating the instance inside the ViewModel, we would actually create it early on in our app architecture and then “inject” the instance into the ViewModel. I haven’t done that in this playlist because I only recently covered Dependency Injection in the Advanced playlist lol
Thanks for the video very insightful! I had a question about core data, the way you had it it set up every time someone added an item it would make a whole new entry basically an array. For my use case I want the user to be able to set one value and have that persist within one object . Is there any modification you would suggest for this or is it just a limitation of entities? not sure if this is clear but I want the user to pick a number and have that be the only data that is saved associated with its key in one persistent object. Hope this is clear Thanks once again
Hey Supreme! Yea, we are basically doing that already. If you ignore the fact that we are saving an array of fruits and just use 1 single fruit, the code would be the exact same.
Hi Nick, great video. How can I set a sort type in the MVVM? Say we have two string attributes defined in one entity. I want to creat two lists, one sorted alphabetically on one attribute and a second list sorted alphabetically in the second attribute.
Hi everyone ! First of all, thanks Nick for what you are doing ! I was asking myself if is it possible to have an array of string [String] in the core data ? Thanks for all Nick !
Nick thanks for your amazing series. I have tried to incorporate cloud kit sync to what you have here, but I am unable to get auto synch to work. Could you cover that topic?
Hi Tim, thanks for the comment! I'm planning on doing a CloudKit series but it won't be for a few weeks. There's so much to cover with CloudKit that it's hard to do a quick video like this. The code structure should be pretty similar to regular Core Data though
@@SwiftfulThinking @Westmaner, II wanted to ask if you came up with a solution until today? I did the same as described in the MVVM video. Afterwards, I wanted to synch with CloudKit. Although, I did all steps necessary for that, the data is not showing in the dashboard. Ideas what I might be missing?
Very good tutorial. Do you know if there is a way or make sense to use @FetchRequest and @FetchedResults with MVVM? Or it's not recommended? Thanks a lot!
Hi, Nick! Thanks for the video! I have one question - why do you use CoreDataViewModel in one file with CoreBootcamp View? Is it possible put CoreDataViewModel to separate file with model file like CoreDataModel and call its methods (add, edit, delete) from separate View file file (like vm.add(), vm.delete() etc) ? Will be published properties (for refreshing list items) work properly in this case? Thanks in advance!
Yes you can totally do that. It's just far faster to demonstrate everything from a single file, and easier for us to see what happening in the context of a RUclips video.
It would be wonderful if you in next videos cover the theme of migration in Core Data. Downloading data from internet with image url and save that data in Core Data with image.
Hi. Do you have a video on how to handle when you inject coredata in the begining and so i have one instace of it in the whole app? I have problem with that ritght now and when i change one thing the other get default values and i get this message in terminal. Accessing StateObject's object without being installed on a View. This will create a new instance each time. I want also to say thank you for you great videos! They really help!
@SwiftfulThinking thanks for awnsering. Have made it and it work fine. To much gpt haha. Thanks again for you good videos. Really good content. I'm a computer science student from Sweden doing a school project so really good to have many great videos for my education :)
My core data functions very well BUT I can NOT figure out how to make textfields that can bind to the entities. I want each entity in the list to bind to textfield in that row that will allow modification of one of the entity's attribute values. I have spent about 20 hours this week trying to figure out this simple issues, any guidance would be SO appreciated. I have watched all of your core data viedos but don't understand how to apply what I know to this specific scenario.
I am struggeling with this. I already have a MVVM project and am trying to use core data. But the new entity conflicts with my existing model. I don't know how to integrate the two so to say.
So 'on update' how would we load the name of the fruit in the list which has been tapped - into the textFieldText so it can be edited and resaved? Please
Shouldn't the CoreData Stuff be separated into a Service class? and then DI EnvironmentObject? I hope there is a proper tutorial for that as sometimes Im confused what is the proper way with MVVM.
Very good video! But what do I do if I want to have two data models? I have tried but the app crashes and I get this error: "The fetch request's entity 0x2812288f0 'TheItems' appears to be from a different NSManagedObjectModel than this context's"
I can't get this architecture to work unless I generate classes for my entities (public class MyEntity: NSManagedObject { etc. etc.}). I'm guessing you did this but I missed it somehow?
Great tutorial but you never used model for your transactions. You could have created an instance of model on any CRUD operation and pass it to the VM instead of on the fly object from UI
Hi. Very nice intro to how CoreData works in conjunction with MVVM. I got two questions: 1. Assuming CoreData container grows bigger and bigger with various entities and entries, is there any downside to performance when using this MVVM approach? Reason I'm asking is that now every time you change something, save etc. you run the full fetch request - not sure if the @FetchRequest maybe is any more efficient in that? 2. How do you usually set up your ViewModels in a larger App? Do you create one big ViewModel? Multiple ones? Do you have any general approach to that?
1. Running the full fetch request can be performance intensive, but Core Data is very powerful and I don't think it would cause performance issues unless there was a very large data set. If you had a large data set, you would probably have a "predicate" (aka filter) on the FetchRequest so that you don't actually fetch ALL the data, but rather a subset. If you did want to improve performance, you could simply save to CoreData and then update the "savedEntities" array manually! The @FetchRequest is probably less efficient overall and not very practical for MVVM apps (or any architecture really).
2. The architecture of any app is pretty unique to the app itself. But generally speaking, I would say every Screen could have it's own ViewModel. However, it multiple screens share the same data, they would probably use the same ViewModel.
Good video, i have a question: is the xcdatamodeld file necessary? Can you define your entities using plain code somehow, avoiding declaring all your entities there?
You can have Xcode generate a NSManagedObject class file from the xcdatamodeld and use those clases if you prefer to work with the code directly, but the data model makes it really easy to make quick changes and Xcode handles all the code changes for you.
Hi Nick sorry to go on BUT when it comes to editing a fruit do we just delete it and make a new one. This cant be right so I ask again how does the editing bit work within the MVVM method? Cheers
Hi Gabriel, our Models are structs, which are based on values. If we change the underlying values, we have to mutate the struct. We can use either a "mutating" function or create a new struct like we do in the video!
@@SwiftfulThinking Hi Nick thank you - can't help thinking that would have been useful shown in the tutorial rather than assuming we want to update with an exclamation mark. The season I watch tutorials is because I don't know how and want to learn. I will try and find another resource. Thanks again
Hi Khaled! You technically can save images to Core Data by first converting them into Binary Data and then saving that. However, it is generally not the recommended approach. I would recommend using the FileManager or an NSCache to persist images. I cover those in videos #26/27 of this series!
@@SwiftfulThinking back in the day in SQL, I stored a Passport photos converted to Binary for a national a Employee app I developed, that allowed many other apps which I also wrote could draw from. I also took most of the photos. haha. The day I retired I shut my brain to all that and learned more about home renovations and carpentry. I built two off the grid cabins [vaulted ceiling & loft, solar, water capture, sauna, hot shower] and now presently I'm working on a large addition to one of them that has a multi pitch hip roof, something new for me. On my nights and early morning hours I'm learning SwiftUI, thanks!
Odd thing. The list will update automatically when I add a new record but it will not refresh when I do an update. I know it is changing the data and saving the data, because went I start typing in the TextField and list refreshes with the update. Weird. I can not understand why this would happen?
@@SwiftfulThinking Thank you... I always ... Now I finish with clone one app and jump to your course in Udemy. You go right way Nick... Thank you for sharing your knowledge .... I like your explanation, clear and different corners
Thanks for this awesome video. Was all very clear but I seem to be having a bit of a brain block on one thing. In the func updateFruit(entity: FruitEntity) on line 49 it says entity.name = newName. Can't wrap my head around that. Doesn't the function need to be mutating to change entity.name from within the function. Hope someone can help me with this.
Mutating is for when you are updating an object that is based on Values (ie. a Struct). CoreData Entities, however, need to conform to NSObject, which require them to be Classes.. which are based on References. Therefore, we don’t need to “mutate” here. (I have an upcoming video on Structs vs Classes that will hopefully make this more clear)
it took me an hour to figure why nothing was working. line 35. I didn't realized you added the 'savedEntities = ' to the 'try container' line. Stupid me.
My comments was deleted because of a link - I found a post on apple dev forums that says "Stop using MVVM for SwiftUI" and suggests encapsulating this logic inside of a view
I litterly spend hours researching this and you saved me in 1/2h. Thank you so much
Happy to help Michael :)
when you create entity in core data model be sure after that to press cmd + s to save cuz Xcode mostly do not save file extension xcdatamodeld automatically
"I'm just doing this again for ppl who are just joining". Proof that heroes don't always wear capes, folks.
Hi Nick! I'm looking for a few adventures in the mobile world as a senior full-stack web developer and thanks to your courses I've been learning a lot! Already built my first simple app and I'm planning to tackle a second one soon. Thanks to your patience, awesome teaching skills, and great content, I feel like I have all I need to go into these waters, as I don't always understand Apple's documentations. Thank you very much for putting such an effort to this, enabling myself and many other people like me.
This is the third time I've been watching this series, and every time I finished the course, I always learn new things! Thank you, Nick!🥳🥳
Though I learned to use CoreData somewhere else, your coding style is much more realistic that I can always learn something. That is why I keep watching your tutorials even on topics that I am familiar with. I am so thankful for your teachings of good qualities. Thank you!!
Haha I love that :) Thanks for watching!
This changed everything for me - first time user of coredata
Glad I found your video - I was really struggling with Core Data in the MVVM pattern - I haven't found anywhere else that explains it so well - Thank you!
It's because MVVM isn't a good idea in SwiftUI it pretty much defeats the purpose of the view struct and property wrappers
I come from a deep background in data warehouses, relational databases, and REST API testing with backend databases. I have worked with both SQL and no-SQL databases. In data warehouse development and testing, the ETL concept is key. The developer typically should create a software design specification with a source-to-target matrix such that any data transformations are addressed. The transformations or the "T" in "ETL" for extract, transform and load can get somewhat complicated. I have seen software defects rooted in transformation errors. So, the MVVM model is ideal in separating the architecture into components such that the ViewModel deals with transformations. So, the MVVM video is great. I appreciate Nick's attention to details in his presentation on how MVVM works. Now, the only area that gets little coverage in my opinion for multiple Core Data tutorials from either Nick or other influencers is the concept of many-to-many tables. In relational SQL, the SQLite database with many-to-many tables should have an ER implementation such that the many-to-many relation uses a hybrid intermediate table with primary keys from both tables. This allows the queries and inner joins that could be done. I was able to implement that easily using MySQL and SQLite with PyQt. I haven't seen any Core Data video that goes from the ER relation of a many-to-many relation with two tables and primary keys and discusses how that "intermediate table" with primary keys from both tables is addressed in Core Data. The Microolap Database Designer web page has this definition "The only way to create many-to-many (N:M) reference between two tables in MySQL is using an intermediate table, which records set association between primary keys of first table and primary keys of second table." I haven't seen a Core Data tutorial that addresses that in its many-to-many tables by going from the ER software design to the code implementation. Usually, the influencers just write code without ever addressing the written design of an ER model. Such development patterns are ripe for errors in my experience with testing data warehouses. I worked with Informatica developers and was trained by a guy with about 45 years of experience in ETL database development.
Update: I found additional resources that help me to understand what is going. The "many-to-many" relationship of table A to table B where an intermediate table C with primary keys from both A and B is replaced by a relation in the graph object model. I found a website that shows the checklist and a couple of other videos. So, I figured out how to make a traditional ER model then convert it to a graph object model which can then be implemented in SwiftUI Core Data. That ER to graph model conversion is usually missing in most Core Data videos. The typical computer science departments focuses on ER for training on databases. Given that graph object diagrams are largely untaught with comprehension, this can lead to design errors badly. The article I found is "RDBMS & Graphs: Relational vs. Graph Data Modeling".
@@manuelsteele8030thanks for that. And you know you can get to the SQL database underlying Core Data if you need to for testing.
I know you probably won’t see this , but thank you so much seriously. I didn’t know where I could learn SwiftUI and where to start but you help out so much . You deserve so much more subs
Hahah you don't know yet... I read all the comments! Thanks for the comment Tasawar. Happy to help!
You always have a video for something I need to learn!
Thank you so much for these videos! You teach really well. :)
Haha thanks for the comment Thaís! :)
Excellent video. I prefer this method rather than the last video.
Thanks Nick! Simple and straightforward solution to the question of replacing @FetchRequest with ViewModel @ Published. It is unclear why Apple itself did not solve this issue, leaving the developers with a good, but primitive @FetchRequest
This is the video I was looking for! Thank you very much 🙂
Thanks Nico!!
Excellent and clear. It was very easy to understand how you fit together View, View models and model.
Thanks for watching Benson :)
Beautiful explanation
Hi Nick, thanks. It was clear for me. What I missed was possibility to sort data as you explained in the previous video.
Oh sorry I didn't mean to miss that haha. It's pretty much the same logic that you can add to the FetchRequest. If you watch the next video, I believe we will cover sorting and filtering using CoreData with the MVVM approach.
Absolutely essential stuff ignored by some paid subscription programs. Thank you.
Thanks for watching :)
Hello! Thank you so much for the cool material! That's what I was looking for my project! You are the best!
I super love your programming style.
Definitely your explaination is very good. Easy to understand. Thanks!
Your explaination is so clear and excellent. Thank a lot.
Brilliant video, super clean!
Your computers fan was running faster than R2D2 at the end of end game
Exactly what I was looking for. Thank you so much!
YES, loved the video, help immensely, I also put this in the code: savedEntities = savedEntities.sorted(by: { $0.name ?? "missing name" < $1.name ?? "missing name" })
to sort .name, so I did learn something. Thanks again
Amazing stuff Nick. A video showing how to connect/save this database to the user's iCloud storage would be really helpful. (note: not iCloudKit)
Thanks Nick. Very good video!
I'm back... round#? ... who can remember. Every new app, time to rewatch this video. Thanks
This vid changed my life thanks bro
Hahaha thanks for watching Daniel
Super helpful! Thank you!
Correct me if I'm wrong. If you were to reference the view model as Observable/stateObject in multiple views you would end up instantiating CoreData multiple times from each view. This behaviour doesn't seem optimal. Solutions may involve declaring it as a struct or passing it down as an environment object from the main view.
Hey bhagyash! You are correct! So in a real app, you could initialize the Core Data class when your app loads and then you would "inject" it into the view model by passing it into the View and then into the ViewModel initializer. However, I haven't covered Dependency Injection on my channel yet!
@@SwiftfulThinking I see, I think it's better to learn the right way from the start especially when dealing with shit like core data.
Awesome!!!
Thank you! You saved my bacon.
Hey, Nick. Not sure if you saw my previous question yet but if you did, I found a solution so please disregard. It appears as if replacing an existing project with a new project of the same name (using the "Replace" button), interferes with the indexing. Creating a completely new project with a new name fixed the problem.
Thanks for everything you're doing. Your videos are an invaluable resource.
I am happy to hear you figure it out! Glad you're enjoying the course. Thanks for watching.
Thanks, Nick! I've been watching many videos on how to implement this, and it's been interesting to see the different styles. I'm guessing that if you had multiple vms that needed to listen to a persistentContainer, you'd put the container in some kind of manager, and then pass the context around to the several vm's using observableObject and @State to keep things in sync?
Hey David! That’s pretty close. I would create a class that has the persistentContainer and all the CoreData functions.. and then create a manager class. The manager class will hold the reference to the core data class. Then pass the manager to your VMs. The VMs call functions in the manager, which is essentially a funnel to call functions in the CoreData class. This creates an abstraction layer that will decouple the app from CoreData
@@SwiftfulThinking awesome! I’ll give that a shot. Thanks!
Hi Nick, loving your videos. I'm using Xcode version 14.3, and for the life of me I cannot figure out how to get the colour literals to work the way shown in your videos. I cannot get the picker with all of the pencils in it to appear. Any tips?
Yes, it seems that color literals no longer work, (in latest Xcode), as they do in Nick’s tutorial. 👎
Thanks for the vid.. Super helpful...
Quick question though since it wasn't included in the video.. The delete func was super simple however should I need to delete all of the data, should I loop on the entity one by one and then delete? Is there any other easy ways to do it like container.viewContext.delete(.all)?
amazing
Great work! Thank you
Thanks for watching!
Hi Nick, thanks for this video! I have a question though. I want to implement Core Data in MVVM way as you do, but what confuses me is when I check the box Core Data when creating new project, there's a file Persistence.swift that has persistent controller, and I can't understand where it belongs. Is that ViewModel? Is it okay that it's struct and not class like you did with CoreDataViewModel? Is there a way I can keep this Xcode automatically created file but keep MVVM architecture like you have? Or should I start from scratch like you do in this video and not make Xcode create that Persistence.swift file?
I am no expert but every course I've taken on Swift every instructor has told us to never use those checkboxes for CoreData or Testing. Not sure if that is so we really learn how to implement those items or if the files created are so problematic in many cases? And I'm talking high-quality instructors like Sean Allen, Paul Hudson, Meng To of DesignCode, Ray Wenderlich's courses I've taken, Stewart Lynch, etc. So I am curious as well. ...
Hey Terry! The Persistance.swift file is just a convenience thing that Apple gives us. If you look at the code in Persistence.swift, almost all of it overlaps with the CoreDataViewModel that we built in this project. In production, we would probably separate the ViewModel logic from the CoreData logic. We do something like that in the next video in this series, where we create a CoreDataManager class, which would be the same as the Persistance.swift basically!
@@SwiftfulThinking Oh, thanks Nick! I didn't see next video, my bad. I see now you've created CoreDataManager and then made an instance of it in the ViewModel! Nice! Exactly what I wanted to know - how that persistence blends in with MVVM! Gonna keep watching that video, very interesting
@@ZeakZac Yes, exactly! That’s going to be another step towards a better approach. After that, instead of crating the instance inside the ViewModel, we would actually create it early on in our app architecture and then “inject” the instance into the ViewModel. I haven’t done that in this playlist because I only recently covered Dependency Injection in the Advanced playlist lol
@@SwiftfulThinking Ohhh, I see. I'll check that video as well then. Thank you!!!
Thanks for the video very insightful! I had a question about core data, the way you had it it set up every time someone added an item it would make a whole new entry basically an array. For my use case I want the user to be able to set one value and have that persist within one object . Is there any modification you would suggest for this or is it just a limitation of entities? not sure if this is clear but I want the user to pick a number and have that be the only data that is saved associated with its key in one persistent object. Hope this is clear
Thanks once again
Hey Supreme! Yea, we are basically doing that already. If you ignore the fact that we are saving an array of fruits and just use 1 single fruit, the code would be the exact same.
Hi Nick, great video. How can I set a sort type in the MVVM? Say we have two string attributes defined in one entity. I want to creat two lists, one sorted alphabetically on one attribute and a second list sorted alphabetically in the second attribute.
Thanks Nick)
Thanks for the support!
Amazing tutorial!
Thanks Totokwa!
Hi everyone ! First of all, thanks Nick for what you are doing ! I was asking myself if is it possible to have an array of string [String] in the core data ?
Thanks for all Nick !
Nick thanks for your amazing series. I have tried to incorporate cloud kit sync to what you have here, but I am unable to get auto synch to work. Could you cover that topic?
Hi Tim, thanks for the comment! I'm planning on doing a CloudKit series but it won't be for a few weeks. There's so much to cover with CloudKit that it's hard to do a quick video like this. The code structure should be pretty similar to regular Core Data though
@@SwiftfulThinking @Westmaner, II wanted to ask if you came up with a solution until today? I did the same as described in the MVVM video. Afterwards, I wanted to synch with CloudKit. Although, I did all steps necessary for that, the data is not showing in the dashboard. Ideas what I might be missing?
Very good tutorial. Do you know if there is a way or make sense to use @FetchRequest and @FetchedResults with MVVM? Or it's not recommended? Thanks a lot!
@FetchRequest doesn't work with MVVM. It's great for getting started but I would probably not use it in production.
@@SwiftfulThinking Cool, thanks a lot!
Hi, Nick! Thanks for the video!
I have one question - why do you use CoreDataViewModel in one file with CoreBootcamp View? Is it possible put CoreDataViewModel to separate file with model file like CoreDataModel and call its methods (add, edit, delete) from separate View file file (like vm.add(), vm.delete() etc) ? Will be published properties (for refreshing list items) work properly in this case?
Thanks in advance!
Yes you can totally do that. It's just far faster to demonstrate everything from a single file, and easier for us to see what happening in the context of a RUclips video.
subscribed! great tutorial
Please Nick if we have Int property witch integer type should I choose?
It would be wonderful if you in next videos cover the theme of migration in Core Data. Downloading data from internet with image url and save that data in Core Data with image.
You should save images to the FileManager and only the image url in CoreData
@@SwiftfulThinking Got it! Thanks 🤙🏽
Quick question: so to follow MVVM pattern, we shouldn't use @FetchRequest wrapper?
Thanks a lot!
This video was badass
Hi. Do you have a video on how to handle when you inject coredata in the begining and so i have one instace of it in the whole app? I have problem with that ritght now and when i change one thing the other get default values and i get this message in terminal.
Accessing StateObject's object without being installed on a View. This will create a new instance each time.
I want also to say thank you for you great videos! They really help!
You would take the Core Data code and move it into its own “CoreDataManager” class and then pass that class into the different ViewModels.
@SwiftfulThinking thanks for awnsering. Have made it and it work fine. To much gpt haha.
Thanks again for you good videos. Really good content. I'm a computer science student from Sweden doing a school project so really good to have many great videos for my education :)
My core data functions very well BUT I can NOT figure out how to make textfields that can bind to the entities. I want each entity in the list to bind to textfield in that row that will allow modification of one of the entity's attribute values.
I have spent about 20 hours this week trying to figure out this simple issues, any guidance would be SO appreciated. I have watched all of your core data viedos but don't understand how to apply what I know to this specific scenario.
Thanks!
I am struggeling with this. I already have a MVVM project and am trying to use core data. But the new entity conflicts with my existing model. I don't know how to integrate the two so to say.
So 'on update' how would we load the name of the fruit in the list which has been tapped - into the textFieldText so it can be edited and resaved? Please
Shouldn't the CoreData Stuff be separated into a Service class? and then DI EnvironmentObject? I hope there is a proper tutorial for that as sometimes Im confused what is the proper way with MVVM.
Very good video! But what do I do if I want to have two data models? I have tried but the app crashes and I get this error: "The fetch request's entity 0x2812288f0 'TheItems' appears to be from a different NSManagedObjectModel than this context's"
Thank you so much 😍
Thanks Christian!
if i am going to use coredata handler(like fetch, add, delete) in viewmodel or model, i couldn't use @fetchrequest property wrappers, am i right?
My Reference 27:00
is it better for the CoreData methods to be a singleton? Or it's own class?
I can't get this architecture to work unless I generate classes for my entities (public class MyEntity: NSManagedObject { etc. etc.}). I'm guessing you did this but I missed it somehow?
Hi!! This class CoreDataViewModel... if i cut this and paste in another file. How can i use this on all my views, it's automatic?
Great tutorial but you never used model for your transactions. You could have created an instance of model on any CRUD operation and pass it to the VM instead of on the fly object from UI
Hi. Very nice intro to how CoreData works in conjunction with MVVM. I got two questions:
1. Assuming CoreData container grows bigger and bigger with various entities and entries, is there any downside to performance when using this MVVM approach? Reason I'm asking is that now every time you change something, save etc. you run the full fetch request - not sure if the @FetchRequest maybe is any more efficient in that?
2. How do you usually set up your ViewModels in a larger App? Do you create one big ViewModel? Multiple ones? Do you have any general approach to that?
Hi Jakob, great questions.....
1. Running the full fetch request can be performance intensive, but Core Data is very powerful and I don't think it would cause performance issues unless there was a very large data set. If you had a large data set, you would probably have a "predicate" (aka filter) on the FetchRequest so that you don't actually fetch ALL the data, but rather a subset. If you did want to improve performance, you could simply save to CoreData and then update the "savedEntities" array manually! The @FetchRequest is probably less efficient overall and not very practical for MVVM apps (or any architecture really).
2. The architecture of any app is pretty unique to the app itself. But generally speaking, I would say every Screen could have it's own ViewModel. However, it multiple screens share the same data, they would probably use the same ViewModel.
Good video, i have a question: is the xcdatamodeld file necessary? Can you define your entities using plain code somehow, avoiding declaring all your entities there?
You can have Xcode generate a NSManagedObject class file from the xcdatamodeld and use those clases if you prefer to work with the code directly, but the data model makes it really easy to make quick changes and Xcode handles all the code changes for you.
@@MacEns Thank you, it s always good to know all the available options
Thanks
Hi Nick sorry to go on BUT when it comes to editing a fruit do we just delete it and make a new one. This cant be right so I ask again how does the editing bit work within the MVVM method? Cheers
Hi Gabriel, our Models are structs, which are based on values. If we change the underlying values, we have to mutate the struct. We can use either a "mutating" function or create a new struct like we do in the video!
@@SwiftfulThinking Hi Nick thank you - can't help thinking that would have been useful shown in the tutorial rather than assuming we want to update with an exclamation mark. The season I watch tutorials is because I don't know how and want to learn. I will try and find another resource. Thanks again
Thanks for the video, I'd love to know how to save images to Core Data
Hi Khaled! You technically can save images to Core Data by first converting them into Binary Data and then saving that. However, it is generally not the recommended approach. I would recommend using the FileManager or an NSCache to persist images. I cover those in videos #26/27 of this series!
@@SwiftfulThinking back in the day in SQL, I stored a Passport photos converted to Binary for a national a Employee app I developed, that allowed many other apps which I also wrote could draw from. I also took most of the photos. haha. The day I retired I shut my brain to all that and learned more about home renovations and carpentry. I built two off the grid cabins [vaulted ceiling & loft, solar, water capture, sauna, hot shower] and now presently I'm working on a large addition to one of them that has a multi pitch hip roof, something new for me. On my nights and early morning hours I'm learning SwiftUI, thanks!
Odd thing. The list will update automatically when I add a new record but it will not refresh when I do an update. I know it is changing the data and saving the data, because went I start typing in the TextField and list refreshes with the update. Weird. I can not understand why this would happen?
Had the same issue. I had This in the view
@State var vm = CoreDataViewModel
Needed to be
@StateObject var vm = CoreDataViewModel
Thank you...
Andrej always staying up to date! Thanks for the support brotha
@@SwiftfulThinking Thank you... I always ... Now I finish with clone one app and jump to your course in Udemy. You go right way Nick... Thank you for sharing your knowledge .... I like your explanation, clear and different corners
@@Mahadev-x7u thanks Andrej! FYI, I made that course a long time ago and I will be posting better courses on here in the next few weeks.
@@SwiftfulThinking very good.... Go Beyond
How to reference it in other view files?
Thanks for this awesome video. Was all very clear but I seem to be having a bit of a brain block on one thing. In the func updateFruit(entity: FruitEntity) on line 49 it says entity.name = newName. Can't wrap my head around that. Doesn't the function need to be mutating to change entity.name from within the function. Hope someone can help me with this.
Mutating is for when you are updating an object that is based on Values (ie. a Struct). CoreData Entities, however, need to conform to NSObject, which require them to be Classes.. which are based on References. Therefore, we don’t need to “mutate” here. (I have an upcoming video on Structs vs Classes that will hopefully make this more clear)
@@SwiftfulThinking Got it! Thanks very much for the response!
it took me an hour to figure why nothing was working. line 35. I didn't realized you added the 'savedEntities = ' to the 'try container' line. Stupid me.
My comments was deleted because of a link - I found a post on apple dev forums that says "Stop using MVVM for SwiftUI" and suggests encapsulating this logic inside of a view
The examples in that post are for simple projects. Don’t listen to that nonsense 😅
String! Not Shtring!
funny