How to use Core Data with MVVM Architecture in SwiftUI | Continued Learning #15

  Переглядів 47,116

Swiftful Thinking

Swiftful Thinking

3 роки тому

In the last video in this series, we learned how to implement Core Data into SwiftUI applications using @FetchRequest. Although that method is more "SwiftUI", it's not always practical to use in production applications. In this video we will learn an alternative approach and connect Core Data to our app using a ViewModel in MVVM (Model-View-ViewModel architecture). As you'll see, the main benefit to this method is that we can separate our code so that the code relating to updating Core Data is totally separate from the code relating to the View (the UI).
If you've watched both videos... which method do you prefer?
Leave a comment below :)
Next video: • Core Data relationship...
Last video: • How to use Core Data w...

КОМЕНТАРІ: 132
@tomqin3685
@tomqin3685 Рік тому
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!🥳🥳
@a.h.m1365
@a.h.m1365 2 роки тому
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
@moisesscn
@moisesscn Рік тому
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.
@philslinkee
@philslinkee Рік тому
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!
@indiekiduk
@indiekiduk Рік тому
It's because MVVM isn't a good idea in SwiftUI it pretty much defeats the purpose of the view struct and property wrappers
@michaelmarx1405
@michaelmarx1405 2 роки тому
I litterly spend hours researching this and you saved me in 1/2h. Thank you so much
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Happy to help Michael :)
@TheCodingRaconteur
@TheCodingRaconteur Місяць тому
This changed everything for me - first time user of coredata
@Yani-jv4pt
@Yani-jv4pt 2 роки тому
Definitely your explaination is very good. Easy to understand. Thanks!
@MrTalhakhan01
@MrTalhakhan01 2 роки тому
Beautiful explanation
@runpress.runningcompetitio2452
@runpress.runningcompetitio2452 2 роки тому
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
@frankz2933
@frankz2933 2 роки тому
I super love your programming style.
@adamkacha3823
@adamkacha3823 Рік тому
Exactly what I was looking for. Thank you so much!
@BSharing09
@BSharing09 Рік тому
Your explaination is so clear and excellent. Thank a lot.
@thaisfernandes7498
@thaisfernandes7498 3 роки тому
You always have a video for something I need to learn! Thank you so much for these videos! You teach really well. :)
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Haha thanks for the comment Thaís! :)
@alexcai4
@alexcai4 Рік тому
Brilliant video, super clean!
@coffeeCatPeanutDust
@coffeeCatPeanutDust 2 роки тому
Thanks Nick. Very good video!
@kjlee7826
@kjlee7826 2 роки тому
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!!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Haha I love that :) Thanks for watching!
@MarvelousLXVII
@MarvelousLXVII Рік тому
Excellent video. I prefer this method rather than the last video.
@phrax
@phrax 2 роки тому
Excellent and clear. It was very easy to understand how you fit together View, View models and model.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for watching Benson :)
@tasawarsaraf2573
@tasawarsaraf2573 2 роки тому
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
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Hahah you don't know yet... I read all the comments! Thanks for the comment Tasawar. Happy to help!
@manuelsteele8030
@manuelsteele8030 10 місяців тому
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.
@manuelsteele8030
@manuelsteele8030 10 місяців тому
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".
@ClintonSiegel
@ClintonSiegel Рік тому
Super helpful! Thank you!
@HilaProdomo
@HilaProdomo 2 роки тому
This is the video I was looking for! Thank you very much 🙂
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks Nico!!
@drewalthage
@drewalthage Рік тому
Awesome!!!
@emrah309
@emrah309 Рік тому
amazing
@Nunny25
@Nunny25 3 роки тому
Hi Nick, thanks. It was clear for me. What I missed was possibility to sort data as you explained in the previous video.
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
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.
@elemento_arts
@elemento_arts 9 місяців тому
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)
@katesychenko
@katesychenko 2 роки тому
Thanks a lot!
@douglasrutledge1325
@douglasrutledge1325 Рік тому
I'm back... round#? ... who can remember. Every new app, time to rewatch this video. Thanks
@mr_Multitask
@mr_Multitask Місяць тому
Thanks!
@abdouett3676
@abdouett3676 2 роки тому
Great work! Thank you
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for watching!
@felipemarques7531
@felipemarques7531 Рік тому
"I'm just doing this again for ppl who are just joining". Proof that heroes don't always wear capes, folks.
@azharuddinkhan1865
@azharuddinkhan1865 2 роки тому
Thanks
@danielwatson6529
@danielwatson6529 2 роки тому
This vid changed my life thanks bro
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Hahaha thanks for watching Daniel
@danfeinstein4424
@danfeinstein4424 2 роки тому
Absolutely essential stuff ignored by some paid subscription programs. Thank you.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for watching :)
@nikodemeroutis
@nikodemeroutis Рік тому
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 !
@Totokwa
@Totokwa 2 роки тому
Amazing tutorial!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks Totokwa!
@happyuser109
@happyuser109 2 роки тому
subscribed! great tutorial
@cinquain0
@cinquain0 2 роки тому
This video was badass
@adamkhan1613
@adamkhan1613 Рік тому
Your computers fan was running faster than R2D2 at the end of end game
@toastrecon
@toastrecon 2 роки тому
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?
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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
@toastrecon
@toastrecon 2 роки тому
@@SwiftfulThinking awesome! I’ll give that a shot. Thanks!
@user-ry6ix2mr8y
@user-ry6ix2mr8y 3 роки тому
Thanks Nick)
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Thanks for the support!
@fvh500
@fvh500 Рік тому
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.
@ChristianHancke
@ChristianHancke 2 роки тому
Thank you so much 😍
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks Christian!
@relax_music668
@relax_music668 8 місяців тому
How to reference it in other view files?
@cliffcri
@cliffcri 11 місяців тому
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)?
@brucknerdevilliers
@brucknerdevilliers Рік тому
Thank you! You saved my bacon.
@mourbatsalim4830
@mourbatsalim4830 Рік тому
Please Nick if we have Int property witch integer type should I choose?
@vncsnts_
@vncsnts_ 7 місяців тому
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.
@thetrinitysolution9631
@thetrinitysolution9631 2 роки тому
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.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
I am happy to hear you figure it out! Glad you're enjoying the course. Thanks for watching.
@andrejkling3886
@andrejkling3886 3 роки тому
Thank you...
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Andrej always staying up to date! Thanks for the support brotha
@andrejkling3886
@andrejkling3886 3 роки тому
@@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
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
@@andrejkling3886 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.
@andrejkling3886
@andrejkling3886 3 роки тому
@@SwiftfulThinking very good.... Go Beyond
@nizamitagiyev705
@nizamitagiyev705 Рік тому
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.
@SwiftfulThinking
@SwiftfulThinking Рік тому
You should save images to the FileManager and only the image url in CoreData
@nizamitagiyev705
@nizamitagiyev705 Рік тому
​@@SwiftfulThinking Got it! Thanks 🤙🏽
@gabrielrml
@gabrielrml 2 роки тому
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
@Westmaner
@Westmaner 3 роки тому
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?
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
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
@christinakrogmann
@christinakrogmann Рік тому
@@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?
@supreme9187
@supreme9187 2 роки тому
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
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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.
@garychen8185
@garychen8185 2 роки тому
Quick question: so to follow MVVM pattern, we shouldn't use @FetchRequest wrapper?
@TheFistRZ
@TheFistRZ Рік тому
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"
@joshk5582
@joshk5582 2 роки тому
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.
@karthikeyanramasamy8302
@karthikeyanramasamy8302 Рік тому
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?
@khaledaldousari3997
@khaledaldousari3997 3 роки тому
Thanks for the video, I'd love to know how to save images to Core Data
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
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!
@douglasrutledge1325
@douglasrutledge1325 2 роки тому
@@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!
@casperolsen8147
@casperolsen8147 Рік тому
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.
@bhagyashingale5663
@bhagyashingale5663 2 роки тому
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.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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!
@bhagyashingale5663
@bhagyashingale5663 2 роки тому
@@SwiftfulThinking I see, I think it's better to learn the right way from the start especially when dealing with shit like core data.
@mpbonaventura
@mpbonaventura Рік тому
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?
@AvantjerTube
@AvantjerTube 11 місяців тому
Yes, it seems that color literals no longer work, (in latest Xcode), as they do in Nick’s tutorial. 👎
@douglasrutledge1325
@douglasrutledge1325 2 роки тому
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
@developerios6096
@developerios6096 3 роки тому
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?
@MacEns
@MacEns 3 роки тому
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.
@developerios6096
@developerios6096 3 роки тому
@@MacEns Thank you, it s always good to know all the available options
@fsdolphin
@fsdolphin 2 роки тому
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!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
@FetchRequest doesn't work with MVVM. It's great for getting started but I would probably not use it in production.
@fsdolphin
@fsdolphin 2 роки тому
@@SwiftfulThinking Cool, thanks a lot!
@wahhmann123
@wahhmann123 2 місяці тому
is it better for the CoreData methods to be a singleton? Or it's own class?
@beejay1337
@beejay1337 3 роки тому
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?
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Hi Jakob, great questions.....
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
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).
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
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.
@ZeakZac
@ZeakZac 2 роки тому
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?
@KimbrellBrad
@KimbrellBrad 2 роки тому
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. ...
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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!
@ZeakZac
@ZeakZac 2 роки тому
@@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
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
@@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
@ZeakZac
@ZeakZac 2 роки тому
@@SwiftfulThinking Ohhh, I see. I'll check that video as well then. Thank you!!!
@adrianomaiaoficial
@adrianomaiaoficial Рік тому
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?
@philslinkee
@philslinkee Рік тому
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?
@gabrielrml
@gabrielrml 2 роки тому
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
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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!
@gabrielrml
@gabrielrml 2 роки тому
@@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
@ben.aka.bigben
@ben.aka.bigben Рік тому
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
@douglasrutledge1325
@douglasrutledge1325 Рік тому
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?
@brianben1
@brianben1 3 місяці тому
Had the same issue. I had This in the view @State var vm = CoreDataViewModel Needed to be @StateObject var vm = CoreDataViewModel
@samarghourab5545
@samarghourab5545 2 роки тому
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.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
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)
@samarghourab5545
@samarghourab5545 2 роки тому
@@SwiftfulThinking Got it! Thanks very much for the response!
@douglasrutledge1325
@douglasrutledge1325 2 роки тому
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.
@natgenesis5038
@natgenesis5038 Рік тому
My Reference 27:00
@krq999
@krq999 Рік тому
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
@SwiftfulThinking
@SwiftfulThinking Рік тому
The examples in that post are for simple projects. Don’t listen to that nonsense 😅
@serhii_rs
@serhii_rs Рік тому
String! Not Shtring!
@marct.1776
@marct.1776 2 роки тому
funny
@serhii_rs
@serhii_rs Рік тому
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!
@ColeDano
@ColeDano Рік тому
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 UKposts video.
SwiftUI - Intro to MVVM | Example Refactor | Model View ViewModel
11:55
Підставка для яєць
00:37
Afinka
Переглядів 116 тис.
Этого От Него Никто Не Ожидал 😂
00:19
Глеб Рандалайнен
Переглядів 4,2 млн
SwiftData Basics in 15 minutes
15:18
CodeWithChris
Переглядів 33 тис.
CoreData в Swift. Разработка приложений
47:55
simpleDEV. Swift c нуля до бесконечности
Переглядів 7 тис.
Build ENTIRE Frontends With ONE Prompt - OpenUI Tutorial
8:59
Matthew Berman
Переглядів 4,7 тис.
ViewModel Explained - Android Architecture Component | Tutorial
8:27
Stevdza-San
Переглядів 106 тис.
Build a SwiftUI Pokedex app for iOS 15 (Clean MVVM Architecture)
33:10
Project Setup and Shared Components | SwiftUI in Practice #1
20:16
Swiftful Thinking
Переглядів 6 тис.