Core Data in SwiftUI Using MVVM Design Pattern

Поділитися
Вставка
  • Опубліковано 22 сер 2024
  • In this video, Mohammad Azam will demonstrate how to implement a SwiftUI app integrated with Core Data using the MVVM Design Pattern.
    GitHub: github.com/aza...
    If you want to support my work then please consider buying one of my courses below. The links below already have coupons attached which will give you the best deal possible. Thank you for your support!
    Buy me a cup of coffee
    ko-fi.com/azam...
    Passive Income for Developers
    www.udemy.com/...
    Core Data in iOS
    www.udemy.com/...
    Flutter and Firebase - Build Real World iOS and Android Apps
    www.udemy.com/...
    Writing Clean Code
    www.udemy.com/...
    The Complete Hands-On SwiftUI Apps Using Firebase
    www.udemy.com/...
    Composable SwiftUI Architecture Using Redux
    www.udemy.com/...
    MVVM Design Pattern in SwiftUI
    www.udemy.com/...
    Server Side Swift Using Vapor 4
    www.udemy.com/...
    Testing iOS Apps - Behavior Driven Development Using Swift
    www.udemy.com/...
    Machine Learning and Artificial Intelligence Using Swift
    www.udemy.com/...
    SwiftUI - Declarative Interfaces for any Apple Device
    www.udemy.com/...
    Flutter and Dart - Build Apps Using MVVM Design Pattern
    www.udemy.com/...
    Mastering RxSwift in iOS
    www.udemy.com/...
    MVVM Design Pattern Using Swift in iOS
    www.udemy.com/...
    Mastering ARKit for iOS
    www.udemy.com/...
    Intermediate and Advanced iOS - Build Real World Apps
    www.udemy.com/...
    The Complete Guide to JSON Parsing in Swift
    www.udemy.com/...
    Data Structures and Algorithms in Swift
    www.udemy.com/...
    Design Patterns in iOS Using Swift
    www.udemy.com/...
    Mastering MapKit for iOS
    www.udemy.com/...
    Machine Learning in iOS Using Swift
    www.udemy.com/...
    Mastering Firebase for iOS Using Swift
    www.udemy.com/...
    Mastering Server Side Swift Using Vapor 3
    www.udemy.com/...
    NodeJS - The Complete Developer Bootcamp
    www.udemy.com/...
    The Complete Guide to Lean Controllers in iOS
    www.udemy.com/...
    #SwiftUI #CoreData #iosdeveloper #iosdev Check out my courses at
    azamsharp.school/

КОМЕНТАРІ • 44

  • @0wang50
    @0wang50 2 роки тому

    Thank you very much! This video solving the notion of viewmodel for me.

  • @_sabot
    @_sabot 3 роки тому +3

    First of all, very nice and clear presentation style 👍
    I like that you don’t do the actual core data stuff directly in your view model. What I don’t like though, is that you're putting everything inside this common CoreDataManager class. That might be because of this very basic example with only one entity, but if you want to teach a good architecture to beginners you should show directly something which is able to scale. If the app will grow with more views, view models and entities this CoreDataManager will get very big and messy soon. Other than that, I would recommend a reasonable architecture should separate the handling from the NSManagedObjectContext from it’s usage. You are also using the singleton pattern here in a way which I wouldn't recommend. For instance, there is no way to use a memory only persistence store if you want to write unit tests.
    What I would suggest:
    - Handle the NSManagedObjectContext in an own class similar to the one Xcode generates if you check CoreData while creating a new project. IMHO it’s okay to make this a singleton as long you use that only as the default parameter inside an init method.
    - Write separate classes for each entity (I call them Repositories) which abstract the CRUD operations and in which you inject the context.
    - Make these repositories each implement a protocol
    - The view model only knows the protocol of the repository
    With this architecture you can write tests for the repo with an memory only core data context and if you want to write tests also for the view model, you can replace the repo with a mock / dummy which doesn’t use core data at all.

    • @azamsharp
      @azamsharp  3 роки тому +3

      Thanks for your comments. I am a believer is baby steps meaning taking small steps to a bigger solution and not overly complicating stuff from the beginning. In my last video, I put all the code in Views just to show how Core Data persistence work. In this video, I used MVVM pattern. As you mentioned our app is simple so a single CoreDataManager would be enough. As the app scale we can have functionality in the model which inherits from the BaseModel. I covered that in my Core Data course on Udemy.
      For Unit Tests, I would focus on domain level tests and then tests everything else using integration and acceptance tests.

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

      is there a repo you could share that i could read to learn from with the implementation you are recommending

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

    Mohammad, I found that my "title" textField input was intermittently being truncated (last character). So, to fix this problem, I added @Published to the title variable in the TaskListViewModel. Some of your viewers may be experiencing the same issue. This fixes it. Many thanks for the video, this is watershed code that can be used over and over in many Core Data applications.

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

    Thank you so much for this video. It helped me a lot to build an application for a job application. I just bought your course at Udemy! And I’m really interested to continue learning more about Core Data with you. I was thinking if you can create some videos building an app using Core Data plus API calls using MVVM on SwiftUI. Maybe explain as well about the folders created on MVVM architecture. Cheers

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

    Thank you

  • @ben.aka.bigben
    @ben.aka.bigben 2 роки тому

    Very helpful Azam. thank you :)

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

    Thank you so much. Very understanding video!

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

    so amazing sir, thank you so much

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

    Thank you Azam for this powerful video!

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

      A trick : watch movies at kaldrostream. I've been using them for watching lots of of movies recently.

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

      @Kristopher Joaquin Definitely, have been watching on kaldrostream for since december myself =)

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

      @Kristopher Joaquin yup, been watching on KaldroStream for years myself :)

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

    Thank you, that was amazing

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

    Hello Mohammad, thanks for the video. It's very helpful. I have a question though. Why do we have a deleteTask function in our view? Is it possible to move it anywhere else if we want our view cleaner, without any functions and such?

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

      deleteTask inside the View is not doing much. It is invoking the delete method on VM.
      PS: My views on using MVVM has evolved and I no longer recommend MVVM architecture for SwiftUI applications. I have written about it extensively on my website. www.azamsharp.com

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

      @@azamsharp Thanks for the answer! I will surely check out the article!

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

    Great Video, ohne question, how do you this automatic insert Hstack?

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

    Thank you Mohammad Azam for this video. It explains a lot but unfortunately you don't provide all CRUD operations (for example, this video is missing how to edit existing tasks)

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

      I may have some other videos on update functionality.

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

      @@azamsharp Thank you! It will be nice to see.

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

      @@azamsharp I have bought both your Udemy courses: "MVVM Design Pattern Using Swift in iOS" and "MVVM Design Pattern in SwiftUI", what the main difference between them? I love this approach and thank to MVVM + CoreData I have started to understand how to use Core Data also.
      When you will create a new video, please don't forget to show the moment when it's best to initialise the Core Data, for example, if the app will use the pre-populated data (for example, a list of English words with translation, or a list of available addresses, for a situation when the app works offline without access to internet, without loading data from any remote database, in some Udemy trainings you provides examples of projects which uses Firestore db from Firebase, but if to create the app which doesn't use any remote databases?)

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

    Nice

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

    I need to do a Crud inside a Crud and pass the ID of the selected row, how do you recommend me to do that? is there anyway you could help me with that? if so how could I contact you

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

    How to delete all rows in one time? Please help^^ thank you

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

    Nice video. I wouldn't mind purchasing the UDEMY class, but, it's not showing the "introductory" cost. I'm not going to spend close to $100 for my "hobby". Do you have a link for the cost shown on this video, or, a discount code to get it at that price? Thanks!

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

      Use this: www.udemy.com/course/core-data-in-ios/?couponCode=5991B32AA46122F868DF

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

    Hi Azam! love love your videos! I learn so much from you. I want to ask you one thing about this video, I usually use core data with fetchrequest property wrapper and I saw it nowhere on this video, how can I sort the data with this technique? i have created one with a date its what I'm gonna use to sort the data on the foreach loop and now I just can't seem to figure it out how can I sort it without @fetchrequest

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

      Check if it has sortDescriptors property. It it does not then use the old way.

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

    Great video, thank you. I have a question - in contrast to the `persistence.swift` file template Xcode generates for a SwiftUI CoreData SwiftUI life cycle app, your `CoreDataManager` is a class, instead of a struct. Is there a particular reason for this? I want to learn how to apply your solutions but in a template generated initially by Xcode. Is it fair to think of both those files as performing the same thing / role in the app? Thanks again for the video. We really need tutorials on SwiftUI+CoreData+MVVM and this is very helpful.

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

      Struct is mainly used for value types but CoreDataManager is not really holding any values. It is more acting like a service hence Class.

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

      @@azamsharp It's very nice to see such a quick response. : ) Can you clarify: Is your CoreDataManager file fulfilling pretty much the same role in your code as PersistenceController is fulfilling in the Xcode template? If I'm starting with the template, would you recommend editing the Persistence.swift file to be like your CoreDataManager, or is it a better idea to create a new file? I wonder because they both have the Singleton and that is what is confusing me some. These lines:
      static let shared = PersistenceController()
      let container: NSPersistentContainer

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

      @@rillieux1792 yes it is the same role

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

    not able to see CoreData in iOS course in udemy business

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

      Maybe Udemy will add it in the future.

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

    why was the update function not shown in the video?

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

      Here is the code for update: github.com/azamsharp/MoviesAppUpdate

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

      @@azamsharp Thank you very much for your fast reply. This is exactly what I missed in your Tutorial „Core Data …“ in Udemy

  • @LukasLukas-kr7oy
    @LukasLukas-kr7oy 3 роки тому

    👍

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

    Could you upload how to update a task?

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

      Just change the values and perform save on the model object

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

    Hello dear author i have bought MVVM Design Pattern in iOS Using SwiftUI course but cant strart learn it because i have lattest version of xcode (and i havent files like AppDelegate and SceneDelegate) thaths why i am getiing a lot of errors in case, Can you rewrite lessons for lattest stable version of xcode. or say how can i get filw structure like your in my project thx

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

      What errors are you facing?