SwiftUI MVVM Tutorial: Simple Example with ObservableObject

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024
  • This SwiftUI MVVM tutorial uses ObservableObject to walk through a simple example of MVVM SwiftUI App Architecture. MVVM uses a Model, View, and ViewModel to allow for efficient development within teams, overall clean code, and is one of the most popular iOS design patterns. In this tutorial, we create and update a SwiftUI List of Game of Thrones characters using this method. This tutorial uses Xcode 13 and Swift 5.5.
    The source code can be found here: github.com/Kos...
    Want to see more?
    The Story Behind My New App: • I Turned This Life-Cha...
    How I'd Learn to Code Today: • How a Senior iOS Engin...
    RevenueCat / In-App Purchases Tutorial: • Easily Make Money with...
    #SwiftUI #MVVM #SwiftUITutorial #iOS15 #AppArchitecture

Комментарии • 92

  • @mustafaokay1302
    @mustafaokay1302 2 года назад +14

    This is the only basic mvvm tutorial for beginners and believe me I searched all the internet in different languages, thanks man this helped me a lot, keep doing it 👍

  • @williams.7314
    @williams.7314 Год назад +2

    Thank you. I come from a .NET background with desktop publishing, so I was looking for an MVVM tutorial. So. Many. People. don't understand well enough to explain it well. That being said, as the rest of the comments suggest, well done sir, well done. Exactly what I needed.

  • @lickmyshoe182
    @lickmyshoe182 2 года назад +1

    Thank you! When you're getting started you need a stripped back explanation of this stuff and everything else I've seen throws you in at the deep end! Nice work.

    • @logankoshenka
      @logankoshenka  2 года назад

      I appreciate that! Glad you like it.

  • @TidalWaveDan
    @TidalWaveDan Год назад +1

    You sir, had by far the most understandable video in the subject. Thank you. I will be subscribing and recommending your channel to others trying to learn.

  • @jamesmeegan2066
    @jamesmeegan2066 Год назад +3

    That was great, exactly what I needed and simple to understand.

  • @MrDaluti
    @MrDaluti Год назад +1

    Very well explained, thank you Sir, simple but effective. Please keep up the good work. 👏

  • @DmitriyChurilov
    @DmitriyChurilov Год назад +1

    Great introduction into the concept. Thank you!

  • @SantiLucero098
    @SantiLucero098 Год назад +1

    Very clear and simple, thank you man!

  • @joaquimmenezes7396
    @joaquimmenezes7396 8 месяцев назад +1

    this tutorial was so cool, i understood concepts better ....

  • @alexdiaz1712
    @alexdiaz1712 2 года назад +2

    This is exactly what I was looking for! Thank you for making this awesome video and making me laugh 😂

    • @logankoshenka
      @logankoshenka  2 года назад +1

      haha glad you like it - welcome!

    • @georgeikwegbu5284
      @georgeikwegbu5284 2 года назад +1

      For real, I laughed all through, now I have to rewatch it again 😩🤣🤣🤣🤣, nice tutorial Logan, thanks

  • @ob6217
    @ob6217 2 года назад +1

    ur light-mode just mademy day brighter and eyes darker😜 btw, thanks a lot sensei 🤝

  • @SOK-gn6hw
    @SOK-gn6hw 2 года назад +1

    How can I change the array with a button. like if Jon Snow change his email and wanna change this in app how can I do that ?

  • @goisborges
    @goisborges Год назад +1

    Amazing tutorial

  • @DaveJacobseniOS
    @DaveJacobseniOS 2 года назад +3

    I couldn’t follow any of this because my eyes were damaged from light mode.
    Jk, this is solid stuff and a great refresher for me👌

    • @logankoshenka
      @logankoshenka  2 года назад

      hahaha hey you're talking to a fellow member of team dark mode so I get it 😅 thank ya

  • @deiv319x
    @deiv319x 6 месяцев назад +1

    I love the subtle satire 😂😂

  • @ob6217
    @ob6217 2 года назад +1

    i wanna ask something, where are those function come from? the people.reverse and the others.. i dont see it.. mind to explain, please??

    • @ob6217
      @ob6217 2 года назад +1

      ah i get it rn, thank youu

  • @glennmiller394
    @glennmiller394 2 года назад +2

    I get it now. Well done.

  • @AntiSmithhh
    @AntiSmithhh 2 года назад +2

    ObservedObject occasionally works sometime. sometime doesn't and shouldn't.
    It should be StateObject in all similar cases. As for me, it's very clearly described in apple documentation.

    • @logankoshenka
      @logankoshenka  2 года назад

      really?

    • @AntiSmithhh
      @AntiSmithhh 2 года назад +1

      @@logankoshenka really!
      If I'm right, ObservedObject is like 'weak' reference and StateObject is like 'strong' one.
      But wrapper implementation keeps object alive in some cases when we are using ObservedObject. So using ObservedObect instead StateObject doesn't lead crushes and problem just stay hidden in simple cases.
      P.S.: thank you for MVVM example.

    • @logankoshenka
      @logankoshenka  2 года назад +1

      Ahhh that makes sense - thank you for this!

  • @johnpill1
    @johnpill1 Год назад +1

    How would you use this approach to access the same data across multiple views, do you require the environment object at that point?

    • @logankoshenka
      @logankoshenka  Год назад +1

      yeah I think environment object is your best bet for that … check out the tutorials that tundsdev has on this, really helpful stuff 💪

    • @johnpill1
      @johnpill1 Год назад +1

      @@logankoshenka I've actually been using the tutorial you mention, but wondered if there was a simpler way!

  • @dmitrygolovin8066
    @dmitrygolovin8066 2 года назад +1

    Nice man! Thanks for all the great content

  • @TheShambi
    @TheShambi Год назад +1

    Hi, wouldn't it make more sense to have the people array inside a model and not the viewModel.
    let's say you have different sets of potential People array. The viewModel will load them in the constructor based on the array you want. What I mean is that the viewModel should have the possibility to load a lot of different models and not include hard coded values?

    • @logankoshenka
      @logankoshenka  Год назад

      yeah I see what you're saying ... with different sets of people, it'd definitely make sense for that data to be available inside of a model.

  • @chingshenchen
    @chingshenchen 2 года назад +1

    Thank you for sharing. I have subscribed your channel. Keep doing it. :)

    • @logankoshenka
      @logankoshenka  2 года назад

      Thank you so much! More videos coming soon :)

  • @kevindesouza6143
    @kevindesouza6143 Год назад +1

    At 5:30 timestamp, you add the ObservableObject Protocol. Using Swift 5 and Xcode 14.1 I get an error there. Basically it wants to add a Generic Type to the Observable object.When I add it says this is not confirm to ObservableOBject Protocol. Can't proceed further till this is resolved. So pls help.
    Also I get an error when I use the Identifiable Protocol.

    • @logankoshenka
      @logankoshenka  Год назад

      hmm, I wonder if this could be related to Swift versions? If I find an answer I'll reply in this thread.

  • @anhhanry7167
    @anhhanry7167 2 года назад +1

    HI, just learn of this mvvm model based on @observable object, this is new to me, thanks for the tutorial... but I need to goto detail view if I tap/click on each view model, could you help ? (pointing to which info to pass )
    because I don't know how to pass data ( each of model ) to the detail view model ( how to declare variable) , thanks

    • @logankoshenka
      @logankoshenka  2 года назад

      Glad it was helpful! I'm not sure I understand what you mean - could you elaborate?

    • @anhhanry7167
      @anhhanry7167 2 года назад

      @@logankoshenka for example > each person ( in the List) if click, I need to display a next screen ( a detail view ) to display more information ( such as a photo, title, salary, department etc..of the person ) theerefore I need to pass the object into this new view meaning the new detail view would require the correct person ( information of object) to display. I would design the new view but I need the info of the person from previous screen which tie to @Observableobject.

  • @arturoescutialopez1900
    @arturoescutialopez1900 Год назад +1

    Nice video. I´m just wondering, is there a reason for ViewModel to be a class and not a struct? you could use @State in the view make viewmodel a struct and handle inheritance with protocols if needed. Why would you need a reference to the viewmodel and not a copy of its values?

    • @logankoshenka
      @logankoshenka  Год назад

      In the case that we're using the viewModel in more than one place, I would want all references to be updated. That's the main reason for using a class in this scenario.

  • @ForhadDreams
    @ForhadDreams 2 года назад +1

    Great example!. can you please a short MVVC video tutorial for fetching API data CRUD[create, get, update, delete]? Please.....

    • @logankoshenka
      @logankoshenka  2 года назад

      Thanks for the suggestion! You may have a typo - it's typically MVVM or MVC.

    • @ForhadDreams
      @ForhadDreams 2 года назад +1

      @@logankoshenka oh! sorry. it's MVVM. Please. :)

    • @logankoshenka
      @logankoshenka  2 года назад

      @@ForhadDreams gotcha!

    • @ForhadDreams
      @ForhadDreams 2 года назад +1

      @@logankoshenka thanks. Hope we will get it soon!

  • @mattmarshall1834
    @mattmarshall1834 2 года назад +1

    Great tutorial. To the point.

  • @myga77
    @myga77 2 года назад +1

    Show us how to create custom swipes with your scrollview list, not with the regular list in SwiftUI
    I can't figure out how to find the index of each cell in a scrollView

    • @logankoshenka
      @logankoshenka  2 года назад

      custom swipe actions like horizontally?

  • @edgarhernandez65
    @edgarhernandez65 2 года назад +1

    thanks, great video.
    I would love for you to make an example by consuming an api
    Saludos desde México

  • @tokero5199
    @tokero5199 Год назад +1

    Thank you for the explanation, I'm not a fan of you importing SwiftUI in the model and the viewModel, you really should keep those UI agnostic.

    • @logankoshenka
      @logankoshenka  Год назад +1

      ah good point, thanks for the feedback 🤝

  • @franziskaschneider8606
    @franziskaschneider8606 2 года назад +1

    Finally a simple example and not unnecessarily complicated. Thanks! I experimented a bit with the code. The array is updated in the view, but if I change the name of a person, the view is not updated. What do I have to do to make this data binding work?

    • @logankoshenka
      @logankoshenka  2 года назад

      Thank you! And just to be clear, are you changing the name of a person in code or while running the app?

    • @franziskaschneider8606
      @franziskaschneider8606 2 года назад +1

      during runtime -> In the PersonViewModel class I added a renameFirstPerson() func.

    • @logankoshenka
      @logankoshenka  2 года назад

      @@franziskaschneider8606 do you mind sharing that function here? I’ll gladly take a look!

  • @tharindudarshana6480
    @tharindudarshana6480 2 года назад +1

    Great tutorial. Thank you

  • @WilfClegg
    @WilfClegg 2 года назад +1

    Thank you for this video, Logan! This makes it very simple to understand the MVVM SwiftUI structure. Other videos seem to confuse the basics because of other processing features (which only serve to conflate the basic MVVM method). I tried adding an array sort, to sort the array by name. I can't seem to get it to work. I don't understand how this simple addition can be so complicated. Is there any advice you might be able to share? Thanks!

    • @logankoshenka
      @logankoshenka  2 года назад

      Thank you, glad you liked it! Where are you calling the sort method?

  • @thirashapw
    @thirashapw 2 года назад +1

    Love it. Keep doing ❤️

  • @tarun_reddy
    @tarun_reddy 2 года назад +1

    Thanks for this king

  • @thedrenkemp124
    @thedrenkemp124 2 года назад +1

    Would you ever make a video on custom keyboard? How to create custom keyboard?

  • @Danyil-ip3ll
    @Danyil-ip3ll 2 года назад +1

    Thank you.Make more videos!!!!Hello from Ukraine 🇺🇦

  •  2 года назад +1

    Thanks Logan ;)

  • @izzzanaaami
    @izzzanaaami 2 года назад +1

    I live in Russia, and I've watched so many video tutorials and courses about it, but most of them are useless because their authors themselves didn't fully understand what they were doing, or they have very little experience, in general, thanks from Russia

  • @followerOfJesus723
    @followerOfJesus723 Год назад +1

    I'm sorry to be the spoil-sport/negative Nancy but, there are tons of videos that show this. Use an actual Networking Service that actually makes an API call. Then show how to create a dependency manager so you don't make the service in the ViewModel. When you declare the viewModel in the View, it should be optional and not initialized. Then use a dependency provider to initialize it, (possibly in the scene or app delegate or whatever) but the responsibility for initializing the service should not be delegated to the view. That isn't MVVM.

    • @logankoshenka
      @logankoshenka  Год назад +1

      I appreciate the feedback! I’m always doing my best with what I know, and this video specifically was meant to be as simple as possible. You should make a video demonstrating this! I’d love to see it in action 🤝

  • @pren5948
    @pren5948 Год назад +1

    Thanks