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

Поділитися
Вставка
  • Опубліковано 14 кві 2021
  • 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...
    🤙 WELCOME BACK 🤙
    WEBSITE: www.swiftful-thinking.com
    DISCORD: / discord
    GITHUB: github.com/SwiftfulThinking/
    SAY THANKS: www.buymeacoffee.com/nicksarno

КОМЕНТАРІ • 137

  • @a.h.m1365
    @a.h.m1365 2 роки тому +18

    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

  • @tomqin3685
    @tomqin3685 2 роки тому +2

    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!🥳🥳

  • @thaisfernandes7498
    @thaisfernandes7498 3 роки тому +5

    You always have a video for something I need to learn!
    Thank you so much for these videos! You teach really well. :)

  • @moisesscn
    @moisesscn Рік тому +2

    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.

  • @michaelmarx1405
    @michaelmarx1405 3 роки тому +5

    I litterly spend hours researching this and you saved me in 1/2h. Thank you so much

  • @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

  • @HilaProdomo
    @HilaProdomo 2 роки тому +2

    This is the video I was looking for! Thank you very much 🙂

  • @Yani-jv4pt
    @Yani-jv4pt 2 роки тому

    Definitely your explaination is very good. Easy to understand. Thanks!

  • @alexcai4
    @alexcai4 Рік тому

    Brilliant video, super clean!

  • @TheCodingRaconteur
    @TheCodingRaconteur 4 місяці тому

    This changed everything for me - first time user of coredata

  • @adamkacha3823
    @adamkacha3823 2 роки тому

    Exactly what I was looking for. Thank you so much!

  • @runpress.runningcompetitio2452
    @runpress.runningcompetitio2452 3 роки тому +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

  • @MarvelousLXVII
    @MarvelousLXVII Рік тому

    Excellent video. I prefer this method rather than the last video.

  • @kjlee7826
    @kjlee7826 2 роки тому +1

    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!!

  • @coffeeCatPeanutDust
    @coffeeCatPeanutDust 2 роки тому

    Thanks Nick. Very good video!

  • @tasawarsaraf2573
    @tasawarsaraf2573 3 роки тому

    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  3 роки тому +2

      Hahah you don't know yet... I read all the comments! Thanks for the comment Tasawar. Happy to help!

  • @frankz2933
    @frankz2933 2 роки тому

    I super love your programming style.

  • @BSharing09
    @BSharing09 Рік тому

    Your explaination is so clear and excellent. Thank a lot.

  • @ClintonSiegel
    @ClintonSiegel Рік тому

    Super helpful! Thank you!

  • @phrax
    @phrax 3 роки тому +1

    Excellent and clear. It was very easy to understand how you fit together View, View models and model.

  • @manuelsteele8030
    @manuelsteele8030 Рік тому

    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 Рік тому

      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".

  • @drewalthage
    @drewalthage Рік тому

    Awesome!!!

  • @MrTalhakhan01
    @MrTalhakhan01 3 роки тому +2

    Beautiful explanation

  • @felipemarques7531
    @felipemarques7531 Рік тому

    "I'm just doing this again for ppl who are just joining". Proof that heroes don't always wear capes, folks.

  • @Nunny25
    @Nunny25 3 роки тому +1

    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 роки тому +1

      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.

  • @cinquain0
    @cinquain0 2 роки тому

    This video was badass

  • @elemento_arts
    @elemento_arts 11 місяців тому

    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)

  • @abdouett3676
    @abdouett3676 2 роки тому

    Great work! Thank you

  • @dugrut1325
    @dugrut1325 2 роки тому

    I'm back... round#? ... who can remember. Every new app, time to rewatch this video. Thanks

  • @katesychenko
    @katesychenko 2 роки тому

    Thanks a lot!

  • @danfeinstein4424
    @danfeinstein4424 2 роки тому

    Absolutely essential stuff ignored by some paid subscription programs. Thank you.

  • @mr_Multitask
    @mr_Multitask 3 місяці тому

    Thanks!

  • @adamkhan1613
    @adamkhan1613 2 роки тому

    Your computers fan was running faster than R2D2 at the end of end game

  • @danielwatson6529
    @danielwatson6529 2 роки тому

    This vid changed my life thanks bro

  • @happyuser109
    @happyuser109 2 роки тому

    subscribed! great tutorial

  • @Totokwa
    @Totokwa 2 роки тому

    Amazing tutorial!

  • @emrah309
    @emrah309 2 роки тому

    amazing

  • @azharuddinkhan1865
    @azharuddinkhan1865 2 роки тому

    Thanks

  • @brucknerdevilliers
    @brucknerdevilliers 2 роки тому

    Thank you! You saved my bacon.

  • @user-ry6ix2mr8y
    @user-ry6ix2mr8y 3 роки тому +1

    Thanks Nick)

  • @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 !

  • @ChristianHancke
    @ChristianHancke 2 роки тому

    Thank you so much 😍

  • @thetrinitysolution9631
    @thetrinitysolution9631 3 роки тому

    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  3 роки тому

      I am happy to hear you figure it out! Glad you're enjoying the course. Thanks for watching.

  • @toastrecon
    @toastrecon 2 роки тому +1

    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 роки тому +3

      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!

  • @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.

  • @gabrielrml
    @gabrielrml 3 роки тому

    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

  • @cliffcri
    @cliffcri Рік тому

    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)?

  • @supreme9187
    @supreme9187 2 роки тому +1

    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.

  • @mourbatsalim4830
    @mourbatsalim4830 2 роки тому

    Please Nick if we have Int property witch integer type should I choose?

  • @nizamitagiyev705
    @nizamitagiyev705 2 роки тому

    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  2 роки тому +1

      You should save images to the FileManager and only the image url in CoreData

    • @nizamitagiyev705
      @nizamitagiyev705 2 роки тому

      ​@@SwiftfulThinking Got it! Thanks 🤙🏽

  • @garychen8185
    @garychen8185 2 роки тому

    Quick question: so to follow MVVM pattern, we shouldn't use @FetchRequest wrapper?

  • @mpbonaventura
    @mpbonaventura Рік тому +1

    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 Рік тому

      Yes, it seems that color literals no longer work, (in latest Xcode), as they do in Nick’s tutorial. 👎

  • @Westmaner
    @Westmaner 3 роки тому +1

    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 роки тому +1

      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?

  • @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"

  • @bhagyashingale5663
    @bhagyashingale5663 3 роки тому +5

    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  3 роки тому +3

      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 3 роки тому +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.

  • @dugrut1325
    @dugrut1325 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

  • @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?

  • @andrejkling3886
    @andrejkling3886 3 роки тому

    Thank you...

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому

      Andrej always staying up to date! Thanks for the support brotha

    • @andrejkling3886
      @andrejkling3886 3 роки тому +1

      @@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 роки тому +1

      @@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

  • @natgenesis5038
    @natgenesis5038 Рік тому

    My Reference 27:00

  • @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.

  • @khaledaldousari3997
    @khaledaldousari3997 3 роки тому

    Thanks for the video, I'd love to know how to save images to Core Data

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +1

      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!

    • @dugrut1325
      @dugrut1325 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!

  • @fsdolphin
    @fsdolphin 2 роки тому +1

    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 роки тому +1

      @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!

  • @ZeakZac
    @ZeakZac 2 роки тому +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 роки тому +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 роки тому +1

      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!!!

  • @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 роки тому +1

      Hi Jakob, great questions.....

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +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 роки тому +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.

  • @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.

  • @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 роки тому +2

      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

  • @wahhmann123
    @wahhmann123 4 місяці тому

    is it better for the CoreData methods to be a singleton? Or it's own class?

  • @WattsnabbAB
    @WattsnabbAB 2 місяці тому

    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
      @SwiftfulThinking  2 місяці тому

      You would take the Core Data code and move it into its own “CoreDataManager” class and then pass that class into the different ViewModels.

    • @WattsnabbAB
      @WattsnabbAB 2 місяці тому

      @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 :)

  • @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?

  • @gabrielrml
    @gabrielrml 3 роки тому

    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  3 роки тому

      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

  • @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?

  • @vncsnts_
    @vncsnts_ 9 місяців тому

    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.

  • @relax_music668
    @relax_music668 10 місяців тому

    How to reference it in other view files?

  • @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

  • @samarghourab5545
    @samarghourab5545 2 роки тому +1

    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 роки тому +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!

  • @dugrut1325
    @dugrut1325 2 роки тому

    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 6 місяців тому

      Had the same issue. I had This in the view
      @State var vm = CoreDataViewModel
      Needed to be
      @StateObject var vm = CoreDataViewModel

  • @dugrut1325
    @dugrut1325 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.

  • @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  Рік тому +1

      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

  • @iLoveAppl3947
    @iLoveAppl3947 Місяць тому

    lots of bad practice in this video that beginners can be influenced... not much of private keyword was used giiz

    • @iLoveAppl3947
      @iLoveAppl3947 Місяць тому

      also what about the previews ? you should either update either delete this nonsense

  • @serhii_rs
    @serhii_rs 2 роки тому

    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 UA-cam video.