SwiftUI MVVM Tutorial: Simple Example with ObservableObject

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ม.ค. 2025

ความคิดเห็น • 93

  • @mustafaokay1302
    @mustafaokay1302 3 ปีที่แล้ว +15

    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 👍

    • @logankoshenka
      @logankoshenka  3 ปีที่แล้ว

      Great to hear :) Thank you!!!

    • @Ikemo4
      @Ikemo4 3 ปีที่แล้ว +1

      Retweet

    • @logankoshenka
      @logankoshenka  3 ปีที่แล้ว

      Love hearing that

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

  • @DmitriyChurilov
    @DmitriyChurilov ปีที่แล้ว +1

    Great introduction into the concept. Thank you!

  • @jamesmeegan2066
    @jamesmeegan2066 ปีที่แล้ว +3

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

    • @logankoshenka
      @logankoshenka  ปีที่แล้ว

      love it - thanks for watching!

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

    • @logankoshenka
      @logankoshenka  ปีที่แล้ว

      I appreciate that, glad you liked it!

  • @SantiLucero098
    @SantiLucero098 ปีที่แล้ว +1

    Very clear and simple, thank you man!

  • @MrDaluti
    @MrDaluti ปีที่แล้ว +1

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

  • @joaquimmenezes7396
    @joaquimmenezes7396 ปีที่แล้ว +1

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

  • @kevindesouza6143
    @kevindesouza6143 2 ปีที่แล้ว +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  2 ปีที่แล้ว

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

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

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

  • @alexdiaz1712
    @alexdiaz1712 3 ปีที่แล้ว +2

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

    • @logankoshenka
      @logankoshenka  3 ปีที่แล้ว +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

    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

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

  • @ob6217
    @ob6217 2 ปีที่แล้ว +1

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

  • @dmitrygolovin8066
    @dmitrygolovin8066 3 ปีที่แล้ว +1

    Nice man! Thanks for all the great content

    • @logankoshenka
      @logankoshenka  3 ปีที่แล้ว

      my guy I appreciate it 🤘🏼

  • @goisborges
    @goisborges 2 ปีที่แล้ว +1

    Amazing tutorial

  • @glennmiller394
    @glennmiller394 2 ปีที่แล้ว +2

    I get it now. Well done.

  • @myga77
    @myga77 3 ปีที่แล้ว +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  3 ปีที่แล้ว

      custom swipe actions like horizontally?

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

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

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

  • @mattmarshall1834
    @mattmarshall1834 2 ปีที่แล้ว +1

    Great tutorial. To the point.

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

  • @TheShambi
    @TheShambi 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      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.

  • @redsunset6181
    @redsunset6181 หลายเดือนก่อน

    thank you

  • @chingshenchen
    @chingshenchen 3 ปีที่แล้ว +1

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

    • @logankoshenka
      @logankoshenka  3 ปีที่แล้ว

      Thank you so much! More videos coming soon :)

  • @d.rew.483
    @d.rew.483 10 หลายเดือนก่อน +1

    I love the subtle satire 😂😂

    • @logankoshenka
      @logankoshenka  10 หลายเดือนก่อน +1

      😄 thank ya

  • @tharindudarshana6480
    @tharindudarshana6480 3 ปีที่แล้ว +1

    Great tutorial. Thank you

  • @tarun_reddy
    @tarun_reddy 3 ปีที่แล้ว +1

    Thanks for this king

  • @thedrenkemp124
    @thedrenkemp124 3 ปีที่แล้ว +1

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

  • @DaveJacobseniOS
    @DaveJacobseniOS 3 ปีที่แล้ว +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  3 ปีที่แล้ว

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

  • @edgarhernandez65
    @edgarhernandez65 3 ปีที่แล้ว +1

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

  • @thirashapw
    @thirashapw 3 ปีที่แล้ว +1

    Love it. Keep doing ❤️

  •  3 ปีที่แล้ว +1

    Thanks Logan ;)

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

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

  • @Danyil-ip3ll
    @Danyil-ip3ll 2 ปีที่แล้ว +1

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

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