Swift - Result Type - Cleaner Network Calls

Поделиться
HTML-код
  • Опубликовано: 28 май 2020
  • Head to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
    In today's video I show you how to use Swift's Result type that was introduced in Swift 5. In this video we clean up an old, ambiguous network call that relied on optionals. Using Swift's Result type eliminates the optionals and makes things very clear at the call site.
    Swift Result Documentation:
    developer.apple.com/documenta...
    If you like my teaching style, I've released my first iOS dev course which is all about the take-home project job interview for iOS developers.
    Course Info - seanallen.teachable.com/
    Link to my book - How I Became an iOS Developer:
    gumroad.com/l/sean-allen-origin
    Hired.com:
    hired.com/x/1n01g
    Check out my podcast, iOS Dev Discussions:
    itunes.apple.com/us/podcast/i...
    Are you a gamer? I'm streaming video games on Twitch:
    twitch.tv/seanallen
    Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
    Ray Wenderlich Books:
    store.raywenderlich.com/a/208...
    Ray Wenderlich Videos:
    store.raywenderlich.com/a/208...
    Paul Hudson's Hacking With Swift:
    gumroad.com/a/762098803
    Learn Advanced Swift Here:
    gumroad.com/a/656585843
    Links to my iOS Dev Setup & iOS Dev Book Recommendations
    www.amazon.com/shop/seanallen
    Personal Links:
    Twitter:
    / seanallen_dev
    Instagram:
    @seanallen_dev
    #swift #softwaredeveloper #iosdeveloper
  • НаукаНаука

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

  • @guitaripod
    @guitaripod 4 года назад +3

    Awesome, Sean. Please do more of these non-beginner videos. Big fan of these

    • @seanallen
      @seanallen  4 года назад +1

      I have a mixture of beginner and non-beginner videos on my to-do list.

  • @hectorsvill
    @hectorsvill 4 года назад +6

    I definitely need to use result type more often 😄

  • @MikaelaCaron
    @MikaelaCaron 4 года назад +2

    Cleaner networking!
    Thanks, Sean great video!

  • @darinburch9043
    @darinburch9043 3 года назад +1

    DUDE THANK YOU

    • @seanallen
      @seanallen  3 года назад

      Happy to help, Will.

  • @TheAhmadReza
    @TheAhmadReza 4 года назад +2

    I had some troubles with passing server direct errors to user with this method but finally it's working, Thank you Sean.

    • @seanallen
      @seanallen  4 года назад

      Happy to hear it helped, Ahmad!

    • @NITINAGAM
      @NITINAGAM 4 года назад

      You can use the enum with an associated value. If you have any other solutions, please share.

    • @TheAhmadReza
      @TheAhmadReza 4 года назад

      @@NITINAGAM Yea I used enum with an associated value...

  • @chiraglakhani5434
    @chiraglakhani5434 3 года назад +1

    Thank you, great tutorial.

  • @masterofyngwie
    @masterofyngwie 4 года назад +2

    Pretty good tutorial, this will make my networking calls a lot more cleaner!

  • @chrisgrayston6407
    @chrisgrayston6407 4 года назад +1

    Thanks Sean, love these tutorials

  • @lianahaque2512
    @lianahaque2512 4 года назад +1

    Loving this content! A lot of stuff to learn during these times to stay productive. Thanks, Sean!

    • @donathmm3881
      @donathmm3881 4 года назад +1

      I think the same! Keep on Sean!

    • @seanallen
      @seanallen  4 года назад

      Glad you like them, Liana!

  • @Ipat639
    @Ipat639 4 года назад +3

    Great 👍, I love this new feature too.
    In error type, better to use errors conforming to LocalizedError protocol

  • @temokaxniashvili2375
    @temokaxniashvili2375 3 года назад

    Very Nice Tutorial 🥰

  • @roushilsingla6325
    @roushilsingla6325 4 года назад +1

    That's a great help. Thank You ⭐️

    • @seanallen
      @seanallen  4 года назад +1

      Glad it was helpful!

  • @ivanputeraa
    @ivanputeraa 4 года назад +1

    Thanks for the tutorial Sean!!!

  • @KelKrom
    @KelKrom 4 года назад +1

    Awesome explanation, thanks Sean.

  • @abdulazizalharbi6228
    @abdulazizalharbi6228 4 года назад +1

    great tutorial and excellent explanation, I really enjoyed watching it, keep it up

  • @finalpets
    @finalpets 4 года назад +1

    Thanks Sean, my network call was incomplete and I didn’t notice that, I definitely you change my code looks more cleaner and easy to read.

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

    Thanks, my man

  • @maxspeed2k
    @maxspeed2k 4 года назад +1

    Thanks Sean!

  • @nawafb6280
    @nawafb6280 4 года назад

    really thank you I need it , made the life cleaner and esaier

  • @JitendraPatel-nb3yf
    @JitendraPatel-nb3yf 4 года назад +1

    Great tutorial, I like it.

  • @vamsi3877
    @vamsi3877 4 года назад +1

    Helpful👏

  • @rrakesh05
    @rrakesh05 4 года назад +1

    Awesome tutorial Sean... :)

  • @juliayamamoto1472
    @juliayamamoto1472 4 года назад +2

    Great video! I usually have two completion handlers: A successful one and a failure one (is that bad?) and I call the one that makes sense. I think I'll change to this solution :) I really liked that! Thanks for the tip. I am a new follower and your channel is helping me a lot!

    • @seanallen
      @seanallen  4 года назад

      Yes, using Result type is now the preferred way in most scenarios since it was introduced. Happy to hear you are enjoying the channel!

  • @guitaripod
    @guitaripod 4 года назад +1

    The timing of this video is superb lol

  • @alexchase1856
    @alexchase1856 4 года назад +1

    I've found this works best when combining Result, DataTask, and Codable with generics. Essentially decoding your network response data as the lhs of your completion closure. I've got plenty of code to share if you want to do a follow up.

    • @seanallen
      @seanallen  4 года назад +2

      Hey Alex, I appreciate that! For context, this sample code is from my take home project course that's aimed at beginner developers looking to get their first job. For that reason, I demonstrated the network calls the "long way" so I could spell everything out and explain all the details for educational purposes.

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

    Good video. But I think depending on the use case, the old way is still valid. Imagine, if you have several method calls
    one after the other, which can fail. So, essentially you "jump" out of the success case right after the switch statement and call the next failable method OR you nest calls in the success case. So, that is were guard let comes into play. You first handle all the failure cases of the method calls and after that you can cleanly code your success case.
    So, it's good that we have another way of dealing with failures, Maybe in future there will be some common way on how to use the new result type in the case of several failable methods

  • @Jock3R87
    @Jock3R87 4 года назад +1

    You nailed it 🪓

    • @seanallen
      @seanallen  4 года назад +1

      Thanks Sagar!

    • @Jock3R87
      @Jock3R87 4 года назад

      Sean Allen following all your videos... just a suggestion that nowadays Unit testing or TDD approach would be useful and shifting focus from OOPS to POP .... if possible can make real time example using protocol oriented programming with use of DI would be helpful for ios people. Not talking about Hello World kind example but actual xCode small tutorial...

  • @soullesessess
    @soullesessess 4 года назад +1

    Great video as always. Also, may I know how do you keep your code formatting so clean? My code is messy and it gets even worse when there is a code snippet copied over from other places. Do you use any tool to format the code or just do it manually? Thanks!

    • @seanallen
      @seanallen  4 года назад +1

      Thanks! I don't use a tool, I just make it a priority to make my code formatted neatly because I'm huge on a readable codebase. Code format is just one part of that.

  • @3ilz
    @3ilz 3 года назад

    10/10

  • @VictorRoldanvictorroldan25
    @VictorRoldanvictorroldan25 4 года назад +1

    Very useful your videos as always. I have only one recommendation. I think isn’t good create a whole method for each API call, you should create a generic type instead so you wouldn’t have many method with the same code.

    • @seanallen
      @seanallen  4 года назад +1

      Thanks! For context, this sample code is from my take home project course that's aimed at beginner developers looking to get their first job. For that reason, I demonstrated the network calls the "long way" so I could spell everything out and explain all the details for educational purposes.

  • @scottatkinson6339
    @scottatkinson6339 4 года назад

    Hey Sean, thanks for the video! Question how do you get your variables so nicely aligned?
    Within the method ScrollViewDidEndDragging the offsetY contentHeight and height variables with the assigned values are spaced out nicely, do you do this manually? Or is it a setting in Xcode that automatically formats them?

    • @seanallen
      @seanallen  4 года назад +2

      I tab them over manually

  • @salithaherath5525
    @salithaherath5525 4 года назад

    Great video sean.! For Network Call you were using class is there any reason for that or we can use struct?

    • @seanallen
      @seanallen  4 года назад +1

      In my case, my Network Manager is a singleton, so it's a class

  • @othmanal-otaibie140
    @othmanal-otaibie140 4 года назад

    Amazing video !! 👍🏼
    Can you please do a video or link us to a source of information about firebase friend invitation request ! For example, user A wants to invite user B as friend, that will not happen till user B accepts the invitation.Currently kinda struggling with friend invitation requests
    Thanks in advance 🙏🏼

  • @gjermundification
    @gjermundification 4 года назад

    @Sean Allen, did your Donny Wals’ book on combine?

    • @seanallen
      @seanallen  4 года назад

      I have it... haven't had a chance to dive in yet, but soon.

  • @cristianm.310
    @cristianm.310 4 года назад

    Very cool stuff. Out of curiosity, what's in the presentGFAlertOnMainThread method?

    • @seanallen
      @seanallen  4 года назад +1

      The example project is from my take home project course. That's a method we created to call our custom alert and show it on the main thread. You can find more information about the course and watch the first hour for free at seanallen.teachable.com

    • @cristianmoisei9689
      @cristianmoisei9689 4 года назад

      Sean Allen thank you, I'll check it out.

  • @dmitriyobidin6049
    @dmitriyobidin6049 3 года назад

    What about exceptions? Why don't we use throws in th e first place?

  • @tolulopeaboyeji5373
    @tolulopeaboyeji5373 4 года назад

    Hi @sean, I’m a big fan of ur videos. I implemented result type in my project and I’ve been having a hard time wrapping my head around it.
    What i do before is i create a model for my 200 and 400 response so as to display a toast containing my response but I’ve been unable to decode and pass the 400 error to my enum.
    I really need your help.

    • @seanallen
      @seanallen  4 года назад

      In this example, we attached a string to our errors. In the networking code (where we call completed with either failure or success), you can check the response.statusCode. So you could do something like if response.statusCode == 401 { completed(failure(.mySpecial401Error)) }. This will pass the failure case with the error attached, which in turn has a string attached that you can display in your toast.

    • @tolulopeaboyeji5373
      @tolulopeaboyeji5373 4 года назад

      Sean Allen thanks for ur reaponse. I think u dont get my question. What i mean is in scenario where i need to decode both 401error and 200 success. Cause at my work place we always display the decoded message from status code == 401 as a toast

  • @julianbdeveloper
    @julianbdeveloper 4 года назад

    Hey Sean, I'm certain you've talked about it before, but what Xcode plugin do you use to align your variables like that at 1:42? If anyone knows could you let me know in the reply section?

    • @seanallen
      @seanallen  4 года назад

      I just tab them over and line them up myself.

    • @julianbdeveloper
      @julianbdeveloper 4 года назад

      @@seanallen That takes some serious dedication. Thanks for the response Sean and keep up the great work. I learn a lot from you.

    • @seanallen
      @seanallen  4 года назад +1

      It does... and you'll find a lot of developers that don't like it for that reason. If you do this in a non-personal project, I would expect some pushback on it.

  • @Cassin1797
    @Cassin1797 4 года назад

    Hey, Sean ! Can you please explain network calls from closure-related point of view? I don’t really get it, it f*cks me a lot, cause I simply can’t grasp this concept. For example:
    How does completion handler work there? Or, this line : guard let self = self else { return }. I tried to google it, and it’s called strongify self, but I don’t understand what it really does... It would be great if you could explain these moments. Thank you.

    • @seanallen
      @seanallen  4 года назад

      Hey Alex, this would be way too much to explain by typing in a comment. I'll add it to my video idea list. Although, I do explain in the course where this code sample is pulled from if you're interested. seanallen.teachable.com.

  • @EmmaKAlexandra
    @EmmaKAlexandra 4 года назад +1

    Just in time to replace all this networking code with combine

    • @seanallen
      @seanallen  4 года назад

      There will still be a TON of UIKit out there for years to come 😀

    • @EmmaKAlexandra
      @EmmaKAlexandra 4 года назад

      Sean Allen I mean you can use Combine with UIKit too!

  • @davidlintin
    @davidlintin 3 года назад +1

    I used to pass in a viewController on a network method so I could present an error, this way is much better!

    • @seanallen
      @seanallen  3 года назад

      Happy to help with a new way to do things, David!

  • @DavidNitzscheBell
    @DavidNitzscheBell 4 года назад

    This is really good, but I disagree that anything is ambiguous. It's verbose, it's messy, but it's not ambiguous. But, basically, it seems to boil down to something parallel to optionals. With optionals, there are two values: nil or not nil, but with not nil, there is an associated value. Same thing here. There are two result values: success or failure. And each one has an associated value.
    Very useful, thanks, but I'd just discuss it differently....

    • @seanallen
      @seanallen  4 года назад

      I see what you're saying, and maybe "ambiguous" was too strong of a word. I wanted to demonstrate how, in my opinion, the call site using Result is MUCH more readable and clear.