The Complete Guide Using SwiftUI MVVM Async Await & SwiftUI Error Handling: SwiftUI Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ต.ค. 2024

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

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

    I can honestly say this is the first video on async/await that really clicked with me - so good. Thanks so much, looking forward to more videos!

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

      Thanks man, really appreciate it. I’ve got more interesting stuff planned/on the way 👀

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

    Best ever swift networking tutorial I've seen on TH-cam. Thanks man.
    Feels like using Flutter and Riverpod Mvvm pattern 💙

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks man glad you enjoyed it 👌🏾

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

    I think this might be the best practices for mvvm async/await in swiftui. I must subscribed!

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

    Great great video about async await. It's the most complete video that also includes response and error handling. Terrific.

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

      Glad you enjoyed man 🫡

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

    This video is actually super great example of Async/Await!!! 🔥🚀Also when u show us how to handle these edge cases with custom errors... It is PARADISE of learning hahaha Thanks a lot mate

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

      You got it!

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

    Another extremely useful and planned out video. Love it.

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

      Thanks Stewart, I appreciate the support 👍🏾

  • @Spacer-l3j
    @Spacer-l3j 8 หลายเดือนก่อน +1

    good stuff my bro people are charging money for this in deprh content we appreciate you my bro

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

      I appreciate that!

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

    I enjoyed this video. Thank you for putting it together. It's nice to see async/await finally make it to Swift to things can be simplified

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

      Thanks, glad you enjoyed it 👌🏾

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

    Thanks a bunch. You do a great job clearly explaining the material.

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks & glad you’re enjoying the content 👍🏾

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

    Thank you for this! Very well explained! You have got yourself a new subscriber!

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks 👍🏾

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

    Thank you for your continued stellar SwiftUI content. Great scope covered and explanations.

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

      Glad you enjoyed it 🙌🏾

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

    Great little summary m8!

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

      Thanks glad you enjoyed it 👍🏾

  • @KhanhQuang-ff2ve
    @KhanhQuang-ff2ve ปีที่แล้ว

    I love you video mvvm with asyn ❤, you are wonderful

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

    Informative, good code structure, and nicely explained. Thank you very much.

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

      Glad you enjoyed it

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

    Thanks. I’m digging into swift right now and I’m kind of surprised that it is only just now getting async await support. I guess my timing is good.

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

      Your timing is very good, though it's not yet available in macOS builds (except on the beta Xcode when running on Monterey beta).

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

    Thank you so much for putting effort into making all these videos on iOS development, Tunde 🙇‍
    I'm relatively new to Swift and I've already learnt a lot! I really like your focus on practical examples, explaining your decisions as you go, solid naming conventions and unit testing.
    Just a small suggestion - you might want to consider recording the screen in a lower resolution to make the videos a bit more smaller screen-friendly. I know a phone is perhaps not the best tool for watching TH-cam and it's great you've increased the font size in Xcode but I think scaling the resolution a bit would make your content that tiny bit more accessible.

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

      Hey glad you enjoyed & thanks for the feedback. Surprisingly i’m not the most technical person when it comes to the technical recording stuff 😅 what are you referring too specifically if the screen resolution? Is the font too small? Too big? Or is it something completely different. Just so I can look into it next time and make some adjustments 👌🏾

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

      @@tundsdev No worries at all, sorry for not making it more clear. Basically, go to `Settings -> Displays -> Display tab` select the `Scaled` Resolution option and choose one of the Larger Text icons. That's all there is to it. 👍

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

    On larger projects do you typically use a DI library or container or do you still manually create services and pass them through constructors?

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

      I’ve saw DI libraries i.e SwiftInject but i’m yet to actually use them on a big project tbh. So I usually do it via constructers 👍🏾

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

    Nicely explained, thanks. Can you do an in depth tutorial with Mapkit/CoreLocation? SwiftUI 3 seems to have some features to implement it easier.

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks 👍🏾 and noted

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

    Which one is more better approach Asyn/Await or Combine ?
    please who see this comment must reply your reply is really helpful for me.

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Both have their own purposes, it depends on your use case

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

    So cool, so much fun. great techniques and explanations. I would love to see how you would implement infinite scrolling in the list, the API returns only first 20 results (671 characters, so not truly infinite). Maybe throw in the searchable modifier and build out the UI a bit. Just thoughts. I'll figure it out, Tanks tunds! Great choice of API. Hyped!

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

      Thanks man, I currently do have a video planned to discuss how i’d tackle infinite scrolling coming out soon so keep your eyes peeled 👀

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

      Also I saw your previous comment too thanks again, not too sure why TH-cam is deleting comments on my videos 🥴

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

      @@tundsdev Nice, can’t wait. In your corner, cheering you on, keep on grindin! Thanks for the time you spend doing these videos, it’ll come back around.

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

      @@tundsdev All good, sometimes I ramble, TH-cam is probably doing some restructuring on the back end or something, maybe a bug somewhere.

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

    Create some more videos on unit tests and dependency injection

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

    Thank you share .

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks 👍🏾

  • @mr.getrighhttt3433
    @mr.getrighhttt3433 ปีที่แล้ว

    This is a great tutorial. I'm curious though, how do I fetch an image from the URL using AsyncImage?

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

      Thanks & I have a video covering this on my channel down below.
      th-cam.com/video/2YVEGfx8jNU/w-d-xo.html

    • @mr.getrighhttt3433
      @mr.getrighhttt3433 ปีที่แล้ว

      @@tundsdev preciate that bro. I'm a native android dev looking to learn ios dev for my job. In Android, we have quite a few dependencies we can use for dependency injection. I noticed you manually injected yours here. Do you have a video on that as well?

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

    Thank you, useful video. My only comments are
    1) any chance of putting the source up on GitHub?
    2) I’m torn on the view knowing anything about the service class at all. I think I need to watch your MVVM project structure & im still need to using MVVM, but I’d have thought better to either have the service class internal to theVM (although the would make mock testing harder), or set up the view model with service class in top level App, and then pass that in as environment object … but then why would any other struct have access to the view model. 🤔

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

      Hey Rob,
      1) I will put it up, just at the moment i'm really busy with work & keep forgetting to do it.
      2) The reason why I separated out the service class is because, when designing the architecture for my apps. I don't like tying the service code directly to view models. I prefer to treat service classes as individual units that can be swapped in and out of different view models.
      This is a really simple example that I did since I didn't wanna over complicate it. But what I would normally do is actually inject the view model into the swiftui view using the environment so the view would only have knowledge of the view model that it needs.

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

    Hey, is it safe to mark the ViewModel with @MainActor, wouldn't that cause the network request to be on the main thread?

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

      Yes you can mark the view model with the annotation and all funcs and props would get on the main thread too. I just tend to mark my funcs individually just so i know which ones will update UI on the main thread 👌🏾

  • @tarun_reddy
    @tarun_reddy 2 ปีที่แล้ว

    Love your videos! Quick question, i have a contacts view which shows a list of the users current contacts and a navigation link to see their pending friend requests. When they click on the nav link it takes them to a list of user requests which they can either accept or delete. The problem I have is that everything works correctly in the database but in the contacts view init i have the getContacts function so it doesn’t automatically update when a user accepts a request I have to go to another screen and then navigate back to it to see the updated list. Is there anyway I can reflect this change in real time?

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

      Thanks, and what are you using for your database? CoreData? Firebase? CloudKit? You want to change your flow a bit here in you want to create a view model, rather than having a getContacts function you want to have a published property, so whenever you perform an action that should refresh the screen you want to call something to reassign a value to the published property to let SwiftUI know that, “Hey i’ve changed 👋🏾”

    • @tarun_reddy
      @tarun_reddy 2 ปีที่แล้ว

      ​@@tundsdev Im using firebase, and i'm also using published properties. Would you mind if i sent you a link to a quick loom video showing you my code and simulator so you can see what I mean and the problem that im facing

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Hey man I actually have a firebase video that shows you how to react to changes to your database, you may want to check this out to see how I react to changes and update my UI using the firebase listener functionality. Hope this helps
      th-cam.com/video/5gIuYHn9nOc/w-d-xo.html

    • @tarun_reddy
      @tarun_reddy 2 ปีที่แล้ว

      P.S. i shared your channel with the coding club here at Texas A&M!

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks man, hope you all enjoy 👌🏾

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

    I didn't quite grasp the error handling part. Could you please clarify where the error message "The Internet connection appears to be offline" is coming from? In your service file, it seems like the only error being thrown is the one related to the 'invalidStatusCode.' So, how are you displaying this message on the screen since there isn't any in the error enum? Thanks in advance!

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

      👋🏾 So basically line 21 in the fetch characters function the error you’re seeing isn’t my custom error its one that comes from URLSession when there is no internet.
      So this is where you get this internet message from its a built in error message from URLSession that is thrown 👌🏾

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

      ​@@tundsdev I understand now. So, all you have to do is use the 'throws' keyword in the function declaration to pass the error to the ViewModel, right? You don't need to use a do/catch block to handle and pass the error to the ViewModel ?

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

      @Iwasee Yh so cos the function is marked as throws, whenever i use try it will catch that error

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

      @@tundsdev Ok thank you for your time, really great tutorial :)

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

    i want to use POST method with parameter, help me

  • @Poroto2107
    @Poroto2107 2 ปีที่แล้ว

    Amazing. Any chance you share the .xcodeproj shown in this video?

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      Thanks, and yep you can get all the source code for all my videos on my GitHub here github.com/tunds/TH-camResources

    • @Poroto2107
      @Poroto2107 2 ปีที่แล้ว

      @@tundsdev Amazing. Thank you so much! Lastly, shouldn't you be throwing CharacterServiceError.failedToDecode if try JSONDecoder().decode(Song.self, from: data) fails for whatever reason? You're only throwing invalidStatusCode on that function. Not sure wether you just forgot to do so or Im just missing something? You declared the error on the enum but actually never threw it.

    • @tundsdev
      @tundsdev  2 ปีที่แล้ว

      @@Poroto2107 yep lol i forgot to use it 😂😅

    • @Poroto2107
      @Poroto2107 2 ปีที่แล้ว

      @@tundsdev Hah gotchu, no worries. Thanks for replying!

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

    There seems to be a lot of bloat in this tutorial. Why are you using view model objects with SwiftUI? The whole point of its design is to let us use value types that can behave like reference types so using an object goes against that. And why are you using Combine’s ObservableObject with async\await?

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

      I have to disagree with you with the term "bloat" and your other points. So let me break them down.
      1. I'm using View Models objects since the title literally says "MVVM" aka Model - View - ViewModel which is a solid design pattern that I personally like to use when working with SwiftUI. In no point in the video do I state that MVVM is the only design pattern to ever exist or you can use, it's just the one I like to use & felt was good for this tutorial.
      2. The reason why I have a view model object is to prevent my SwiftUI views from becoming bloated with loads of @State properties and variables, moving them out to a view model object allows me to separate concerns and also in the future I could unit test this view model directly to ensure it is behaving properly.
      3. I'm using ObservableObject in relation to point two, the flow is my view starts my async task -> view model interacts with the service class -> service class returns data from the REST API -> view model holds data updates from the service -> view listens to published updates and reinvalidates itself with any necessary updates.
      4. Like I said in the video this is a simple example, I feel like this is a good starting point for new devs using SwiftUI & MVVM who are looking for a good example.
      This comment that you wrote feels like don't agree/like MVVM rather than understanding why I did what I did lol.

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

      @@tundsdev regarding 2, those are valid reasons for using a separate struct, not an object.

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

      @@tundsdev regarding 3, ObservableObject and @Published with assignTo are features of Combine. If using async/await or task modifier then it’s all unnecessary because SwiftUI automatically stores the async context, a state object is not necessary.

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

      @@tundsdev regarding 1, SwiftUI View structs hold the view data and thus are the view model already. Adding another layer of complexity of view model objects creates problems, e.g. strings that are using formatters won’t auto update if region settings change like they do if you use the Text struct with a formatter.

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

      Again, i’d have to disagree. For me personally this would lead to you having a lot of State variables in your views, also this would be hard to unit test also. Separating out your properties into a view model, not only allows you to group your variables which will trigger UI updates. It allows you to also teat your view models to ensure they reinvalidate your views. Again if you’re someone who wants to store all your properties in State variables then more power to you. But me personally i don’t like this approach.