iOS Dev 34: MVVM Design Pattern Explained with Example | Swift 5, XCode 13

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • In this video we learn about the Model View ViewModel (MVVM) Design Pattern.
    We start by understanding how each component connects with themselves, then we implement an example project (Login Page) using the MVVM pattern.
    We also learn how to bind our View with our ViewModel using the Observable method.
    Additional Materials:
    Starter Project: github.com/EMacco/ios-tutoria...
    Generics: • iOS Dev 14: Generics f...
    Concurrency: • iOS Dev 15: Concurrenc...
    Retain Cycle: • iOS Dev 16: Retain Cyc...
    Food Ordering App: • Food Ordering App (Yum...
    Say Hi on Social Media:
    LinkedIn: / ​​
    Twitter: / its_macco
    Video Content
    1. Introduction 00:00
    2. What is MVVM? 00:18
    3. Starter Project Explained 08:38
    4. ObservableObject Class 14:20
    5. Create Network Service 22:16
    6. Implement LoginViewModel 26:00
    7. Implement Binder 30:52
    8. Test Example 37:22
    9. Implement HomeViewModel 39:44
    10. Conclusion 46:25
  • Věda a technologie

Komentáře • 104

  • @followerOfJesus723
    @followerOfJesus723 Před 2 lety +16

    I mentioned you as being a great resource for beginning iOS developers. You do have the BEST explanation of MVVM on CZcams for people who are just starting out. Keep up the great work.

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +1

      Thanks for the kind words, Kwaku
      I appreciate it 😊

  • @DaisakuEjiri
    @DaisakuEjiri Před 2 lety +3

    This saved my day! I like your teaching style that's keeping the boilerplate code to minimum and is straight to the point! Great tut!

  • @easthastings6919
    @easthastings6919 Před 2 lety +3

    one of the best IOS channels out there, so underrated man.. commenting to make YT algorithm stronger

  • @zachdavis8398
    @zachdavis8398 Před 2 lety

    Don't stop doing these, you're tutorials are my favorite

  • @thagionjack8132
    @thagionjack8132 Před rokem

    Great video bro! Your explanation is very easy to understand! Keep doing these!! Thank you!!

  • @kindofexistentialist
    @kindofexistentialist Před rokem +1

    This explanation finally set everything straight. Thank you!

  • @damsobaba6321
    @damsobaba6321 Před 2 lety +1

    Merci beaucoup Emmanuel !! So nice to finally get it !! it would be so good to hear your approach on RxSwift or combine :)

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      Je vous en prie, Damsobaba 😊
      I actually have a video that covers MVVM and Combine.
      You can check that out here.
      czcams.com/video/30LapcVtyBQ/video.html

  • @user-bx2vb9ix2i
    @user-bx2vb9ix2i Před 2 lety

    Thank you so much for your work!

  • @Sakshisharma-kt1mt
    @Sakshisharma-kt1mt Před 6 měsíci

    very nicely explaining the MVVM architecture.

  • @ArdakDev
    @ArdakDev Před 2 lety +1

    Many thanks, that's the best explanation I've seen

  • @mayarabdulkareem8299
    @mayarabdulkareem8299 Před 9 měsíci

    Best MVVM tutorial!

  • @emanuelditzel5190
    @emanuelditzel5190 Před 2 lety

    Thanks for the time and effort you put to these videos, greetings from an Emanuel of Argentina!

  • @abanoubashraf1308
    @abanoubashraf1308 Před 2 lety +2

    been looking for a clear tutorial for so long, thanks a lot bro, please make a video about mvvm with cordinator pattern

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +2

      Coordinator pattern is something I have in mind.
      Keep an eye out for it 😊

    • @tokero5199
      @tokero5199 Před 2 lety

      Those are two different patterns no need for that video. When developers begin to make acronyms like MVVM-C it's because they think that MVVM is the architecture of the app, it's not. It's a pattern designed to solve a specific issue and it should be used at such.

  • @AlexPeresedov
    @AlexPeresedov Před rokem

    You are amazing. Thank you for big font and your style of teaching!

  • @andreipripa9977
    @andreipripa9977 Před rokem

    Very good explanation with appropriate examples, but it will be very handy if you would post also end version of project

  • @abnglobalnews
    @abnglobalnews Před rokem

    Best resource for IOS development !

  • @iOSDevAnjali
    @iOSDevAnjali Před měsícem

    Very Informative!!

  • @ivesmurillo7674
    @ivesmurillo7674 Před 2 lety

    Really great stuff brotha

  • @wandahu8058
    @wandahu8058 Před rokem

    Very good, I learn a lot!

  • @user-tw6yj2mm4o
    @user-tw6yj2mm4o Před rokem

    Great explanation!

  • @chovuse
    @chovuse Před 2 lety

    Great work !!!

  • @raychen4458
    @raychen4458 Před 2 lety +1

    hello Sir, I accidentally bumped into your channel and I have to say that I really enjoy your amazing teaching style and would love to see you make RxSwift and Combine !
    Also I hope you can make a in depth video about the situation where it allows multiple binding happens. Appreciate it

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +2

      Thanks, Ray. 😊
      I would really love to make a video about RxSwift but unfortunately it’s not a short term plan.

    • @raychen4458
      @raychen4458 Před 2 lety

      @@EmmanuelOkwara that is fine. I appreciate your content. just one quick question, is it necessary to use binding method in MVVM or is there other ways to update the UI as well ?

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +2

      There are various other ways. You could use delegates, observers, closures,…
      But these methods will require you to manually notify the listening views.
      If you’re not careful, you may also find yourself referencing the View.
      *Your ViewModel should not depend on your view*

  • @thattechemmanuel
    @thattechemmanuel Před 2 lety

    This is indeed an eye opener.

  • @user-jd7fi4hr9g
    @user-jd7fi4hr9g Před rokem

    Thank's man! it's great👍

  • @ChathuraLakmalBest
    @ChathuraLakmalBest Před 2 lety

    This is really Good Video. Thank you

  • @_ritesh305
    @_ritesh305 Před 9 měsíci

    awesome explanation

  • @GK-ee7mw
    @GK-ee7mw Před rokem +1

    Great video, very useful. One comment on the error observable - it seems like a not great pattern to observe the error and take an update with nil to mean logged in; we could have a bug if say the error was re-initialized to nil and the login hadn’t actually even been attempted yet, cause nil is the default state. What I’d suggest would be something like a isLoggedIn property on the view model that is defaulted to false, and observe that to decide to proceed if it gets set to true

  • @edorfs
    @edorfs Před 2 lety

    Thanks for this great video :)

  • @deedan821
    @deedan821 Před 2 lety

    Excellent explanation

  • @ihemjiereokwara6257
    @ihemjiereokwara6257 Před 2 lety

    Well done Emmanuel 👏👏

  • @preetipandey3905
    @preetipandey3905 Před rokem

    Hi Emmanuel, This was indeed a very good explanation, but one thing i want to point out that, whenever you initialize your view model, and you have subscribe it in setup binder, then Observable object will call with nil value as well. Since you have used listener(value) in bind method as well. A simple print statement would do. Even before clicking on button goToHomePage method will call.
    Let me know if i am missing something.

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před rokem

      Thanks for catching that, Preeti
      In this case I would remove the listener(value) call in the bind function so that it only sends out the notification when the value changes after binding.

  • @user-ln9be2dq9z
    @user-ln9be2dq9z Před rokem

    awesome

  • @evarilci
    @evarilci Před rokem

    thank you

  • @tahaereneren7809
    @tahaereneren7809 Před 2 lety

    Wait new full project bro thank you

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +2

      I’ll start a new project next year using MVVM design pattern.
      Keep an eye out for it 😊

  • @AmitThakur-eg8kb
    @AmitThakur-eg8kb Před 2 lety

    Hi Emmanuel, what a wonderful way of teaching you have. This video is really very awesome. Actually I want to say, the stuffs which you explained is understandable, but problem arises when dealing with new project from scratch, so you saying that you will write new project in MVVM pattern so it's already been a long time please upload that MVVM project, desperately waiting. Please.

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      Thanks, Amit.
      I’m sorry I haven’t been able to make the video yet, I’ve been really preoccupied.
      I still have it in mind tho, and will create and release the tutorials as soon as I can. 🙏

    • @AmitThakur-eg8kb
      @AmitThakur-eg8kb Před 2 lety

      @@EmmanuelOkwara Hey Emmanuel hope you doing good. Did you find time to create the video, it is already two months now.

  • @diakojafari6020
    @diakojafari6020 Před 2 lety

    vert good

  • @ivanramirez1465
    @ivanramirez1465 Před 2 lety

    sick intro dude

  • @followerOfJesus723
    @followerOfJesus723 Před 2 lety

    Please make an updated video where you show an array of listeners in an observable object.

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před rokem +1

      I’ll consider it in future, in the meantime, you can basically convert it to an array, append to the array, and loop through the array when you want to trigger a notification.

  • @CyberMew
    @CyberMew Před 2 lety

    Nice short tutorial. What about the testing portion?

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      I have a video on testing in general but maybe in future I make more extensive videos.
      The core concept remains the same regardless.

  • @LeviM337
    @LeviM337 Před 2 lety +1

    Awesome video bro! Can you do Viper next?

  • @shinitiomit1095
    @shinitiomit1095 Před rokem

    Thanks for good Video! I started MVVM pattern recentry, this video was so good for me! Explained stroy was so good understanding.
    I want to know about one thing,Beginning of this video ,you said listener(s) get this →[  ] .
    How to attach it this code I didn't know...
    And main storyboard was there,should I make it in MVVM pattern ?
    If you have a time please teach me about this pattern in new video I think!
    Anyway I wish your new videlo anything!
    Thanks!!

  • @jameelshehadeh9011
    @jameelshehadeh9011 Před 2 lety

    great explanation emmanuel is this binding technique called the "Box" Technique?

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před rokem +1

      I’m not sure what “Box” technique is 😬

    • @jameelshehadeh9011
      @jameelshehadeh9011 Před rokem

      @@EmmanuelOkwara it is the same thing as property observers used by ray wanderlich just different name

  • @abdorizak
    @abdorizak Před 2 lety

    Please make a full video about network services encoding data GET POST PUT DELETE ✅

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +1

      I have a video covering GET and POST examples.
      czcams.com/video/xsfzGt7k0rI/video.html
      You can also check out this video teaching how to encode and decode your data
      czcams.com/video/4TfCtY9NINk/video.html

  • @divyavignesh68
    @divyavignesh68 Před 2 lety

    Pls put video on RXSwift

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      I’ll add this to my TODOs.
      Thanks for the suggestion, Divya 😊

  • @Jock3R87
    @Jock3R87 Před 2 lety

    Hi Mate...Can you please make a video on MVVM-C with data passing. It will be great help. Thanks in advance

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      I have this in mind, Sagar.
      Thank you 😊

    • @Jock3R87
      @Jock3R87 Před 2 lety

      @@EmmanuelOkwara Thanks mate.. Urge you to demonstrate how we can pass the data as well… I am but confused cause if coordinator gonna take care of navigation and passing data then what is the use of VM.. maybe silly query but yeah i have that 😊

  • @subinkk1000
    @subinkk1000 Před rokem

    Passing data from view to another view's viewModel - Users List Page -> User Detail. How we pass data from view controller -> Detailed View controller ? - > Directly we can assign from Main to Detail view?

  • @kangajankuganathan4363
    @kangajankuganathan4363 Před 7 měsíci

    what is the difference between [weak self] and [weak self] in

  • @user-bb1ke2sw1j
    @user-bb1ke2sw1j Před rokem

    Hosssom Tutorials...!!!

  • @maulikshah6461
    @maulikshah6461 Před 2 lety

    hello sir, I like your way but i have query on error and model in same bind?
    i can check error in same
    if no error then same bind i use for model
    is it possible to use same bind?

  • @nalindawickramarathna8730

    Thanks for the great tutorial. One question, For a example If I want to populate tableview with list of data in HomeViewController, can I use the same HomeViewModel? Or do I need to create another ViewModel class for this?

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +2

      You are a programmer, you can do absolutely anything 💪😎
      On a serious note tho…It’s best for each view to have its own ViewModel, so for your tableview, the cell will have its own ViewModel that accepts only data that it needs.

  • @abdorizak
    @abdorizak Před 2 lety

    🔥✊

  • @JohnLiuFromHK
    @JohnLiuFromHK Před 2 lety

    Amazing. There are a lot of mvvm tutorials on CZcams, but I still think this is the best one. Clear and to the point but not too fast. ;)
    But I have 2 questions:
    1. In others mvvm tutorials, some of them say that the controller is still here and the controller owns the view model. But when you explain mvvm, nothing seems to own the view model, its just 3 components. Can you clarify this a little bit?
    2. I am studying for interview for iOS developer, just thinking it would be a great idea if you can make a video about interview mock questions in the future?
    Anyway, thank you ;)

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      Thanks, John 😊
      1. I mentioned that in UIKit, we usually have the View and Controller together as ViewController. In this sense, you can choose to say that the controller owns the ViewModel.
      I mostly said View just to avoid introducing the word “Controller” which is not included in the acronym “MVVM”
      2. Best of luck on your interview 💪😁
      Also, I’ve really considered creating a series dedicated to interview preparations. I’ll definitely get to that next year.
      Thanks for the suggestion 😊

  • @greyabeing443
    @greyabeing443 Před 2 lety

    are your videos standalone or part of one project?

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      This particular video is part of a series called iOS Development with Swift
      Here’s a link to the full playlist.
      czcams.com/play/PLgBVHL8joMCslhJPyp2Wzzh5ZO9bmRkK-.html

  • @kwameagyenim-boateng2968

    when added binders, my controller automatically moved to the new vc on viewDidLoad. what might be the problem? I'm using a UINavigationController

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety +1

      A signal is sent immediately you bind the view and view model.
      You could either add a check to confirm the value before navigating to the second page, or prevent the initial signal from firing when binding is initiated. (We added this in 19:34)

    • @kwameagyenim-boateng2968
      @kwameagyenim-boateng2968 Před 2 lety

      @@EmmanuelOkwara Fixed it. Thanks bro

  • @JLCodes
    @JLCodes Před rokem

    @ around 25:00 of this video, you make a `weak reference` to `self` on the DispatchQueue class that executes asynchronous task after few seconds. I wonder why? Since the `NetworkService` is a Singleton class therefore it never gets deallocated. You only use weak references to objects to make sure they are deallocated when they needed to be.
    Also.. I don't think that `DispatchQueue` class causes retain cycles. Although `asyncAfter` causes a temporary cycle, it does not cause memory leaks. You only have a cycle for 2 seconds and after that the cycle is broken.
    Looking forward for your thoughts about my comment. Thanks

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před rokem

      Thank you so much for the detailed response.
      I agree with all you’ve said.
      Using weak self is just a personal habit of mine. Rather than thinking about whether a retain cycle could be introduced or not, I always default to just making a weak reference especially when dealing with closures.
      In my opinion it’s just safest.

  • @Rahul-jf5kf
    @Rahul-jf5kf Před rokem

    Hi Emmanuel lets say on ViewDidLoad I want to call webservice which returns list of movies with each movie contains 20 fields but i want to display only 4 details out of it so in that case do we need to create another structure with 4 fields and map first list with 20 fields to new one with 4 fields and this seems lengthy. Instead if we use same structure reduces code.
    what is recommended way and what are the advantages

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před rokem +1

      Hello, Rahul
      You don’t need to modufy/create a new struct.
      You can simply set a maximum value for numberOfRowsInSection
      This way, it’ll display first X items while your full data is still available

    • @Rahul-jf5kf
      @Rahul-jf5kf Před rokem

      @@EmmanuelOkwara thanks

  • @arunbenjamin168
    @arunbenjamin168 Před 2 lety

    Great tutorial, Can you make a video for multiple subscribers subscribing so that it will make better understanding.

    • @EmmanuelOkwara
      @EmmanuelOkwara  Před 2 lety

      Sure. I’ll add it to my todos 😊
      Happy New Year, Arun 🥳 🎆

    • @arunbenjamin168
      @arunbenjamin168 Před 2 lety

      @@EmmanuelOkwara Thanks Emmanuel.

  • @pallasivaji6035
    @pallasivaji6035 Před rokem

    Great video to learn MVVM..@Emmanuelokwara Thanks

  • @navr3505
    @navr3505 Před rokem

    good video but let me point out this is not actually an MVVM pattern that you are following. MVVM states that ViewModel is responsible for converting models into displayable objects and any logic that goes into converting a model into a viewable object should be handled by ViewModel itself. Hence your ViewModel should always have a reference to a View and a Model and it should be responsible for your rendering out model onto your view that is passed onto the ViewModel from your Controller. What you are doing is again passing that functionality back to your view controller and I don't understand why and how this is MVVM? Please first learn yourself about what actually MVVM is. I can suggest a few courses from very PRO developers who also work directly with Apple. But sorry to say this is a very bad implementation of MVVM.
    You already have created user model to encapsulate user data and then you are declaring more variables to pass that information back to your view controller which is so stupid. Why the actual fkc your viewcontroller needs to see user model and any logic that is required for user model to be displayed on to your view? You have welcome message and then you are creating another string variable to hold that message? What the actual heck are you trying to do by complicating things without any reason.