Swift - Result Type - Cleaner Network Calls

  Рет қаралды 20,248

Sean Allen

Sean Allen

4 жыл бұрын

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

Пікірлер: 83
@guitaripod
@guitaripod 4 жыл бұрын
Awesome, Sean. Please do more of these non-beginner videos. Big fan of these
@seanallen
@seanallen 4 жыл бұрын
I have a mixture of beginner and non-beginner videos on my to-do list.
@MikaelaCaron
@MikaelaCaron 4 жыл бұрын
Cleaner networking! Thanks, Sean great video!
@hectorsvill
@hectorsvill 4 жыл бұрын
I definitely need to use result type more often 😄
@TheAhmadReza
@TheAhmadReza 4 жыл бұрын
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...
@lianahaque2512
@lianahaque2512 4 жыл бұрын
Loving this content! A lot of stuff to learn during these times to stay productive. Thanks, Sean!
@donathmm3881
@donathmm3881 4 жыл бұрын
I think the same! Keep on Sean!
@seanallen
@seanallen 4 жыл бұрын
Glad you like them, Liana!
@chiraglakhani5434
@chiraglakhani5434 3 жыл бұрын
Thank you, great tutorial.
@chrisgrayston6407
@chrisgrayston6407 4 жыл бұрын
Thanks Sean, love these tutorials
@seanallen
@seanallen 4 жыл бұрын
Glad you like them!
@masterofyngwie
@masterofyngwie 4 жыл бұрын
Pretty good tutorial, this will make my networking calls a lot more cleaner!
@seanallen
@seanallen 4 жыл бұрын
Great to hear!
@Ipat639
@Ipat639 4 жыл бұрын
Great 👍, I love this new feature too. In error type, better to use errors conforming to LocalizedError protocol
@abdulazizalharbi6228
@abdulazizalharbi6228 4 жыл бұрын
great tutorial and excellent explanation, I really enjoyed watching it, keep it up
@seanallen
@seanallen 4 жыл бұрын
Thanks Abdulaziz!
@ivanputeraa
@ivanputeraa 4 жыл бұрын
Thanks for the tutorial Sean!!!
@seanallen
@seanallen 4 жыл бұрын
Happy to help!
@KelKrom
@KelKrom 4 жыл бұрын
Awesome explanation, thanks Sean.
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it
@roushilsingla6325
@roushilsingla6325 4 жыл бұрын
That's a great help. Thank You ⭐️
@seanallen
@seanallen 4 жыл бұрын
Glad it was helpful!
@Twenytoo
@Twenytoo 2 жыл бұрын
Thanks, my man
@temokaxniashvili2375
@temokaxniashvili2375 3 жыл бұрын
Very Nice Tutorial 🥰
@finalpets
@finalpets 4 жыл бұрын
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.
@seanallen
@seanallen 4 жыл бұрын
No problem 👍
@rrakesh05
@rrakesh05 4 жыл бұрын
Awesome tutorial Sean... :)
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it!
@darinburch9043
@darinburch9043 3 жыл бұрын
DUDE THANK YOU
@seanallen
@seanallen 3 жыл бұрын
Happy to help, Will.
@vamsi3877
@vamsi3877 4 жыл бұрын
Helpful👏
@maxspeed2k
@maxspeed2k 4 жыл бұрын
Thanks Sean!
@seanallen
@seanallen 4 жыл бұрын
No worries!
@JitendraPatel-nb3yf
@JitendraPatel-nb3yf 4 жыл бұрын
Great tutorial, I like it.
@seanallen
@seanallen 4 жыл бұрын
Thank you! Cheers!
@nawafb6280
@nawafb6280 4 жыл бұрын
really thank you I need it , made the life cleaner and esaier
@Dragonboh1
@Dragonboh1 8 күн бұрын
keep doing what you doing
@seanallen
@seanallen 6 күн бұрын
Will do!
@guitaripod
@guitaripod 4 жыл бұрын
The timing of this video is superb lol
@seanallen
@seanallen 4 жыл бұрын
Glad to hear that!
@juliayamamoto1472
@juliayamamoto1472 4 жыл бұрын
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!
@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 жыл бұрын
You nailed it 🪓
@seanallen
@seanallen 4 жыл бұрын
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...
@alexchase1856
@alexchase1856 4 жыл бұрын
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 жыл бұрын
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.
@3ilz
@3ilz 3 жыл бұрын
10/10
@VictorRoldanvictorroldan25
@VictorRoldanvictorroldan25 4 жыл бұрын
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 жыл бұрын
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.
@soullesessess
@soullesessess 4 жыл бұрын
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 жыл бұрын
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.
@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 🙏🏼
@dmitriyobidin6049
@dmitriyobidin6049 3 жыл бұрын
What about exceptions? Why don't we use throws in th e first place?
@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 жыл бұрын
In my case, my Network Manager is a singleton, so it's a class
@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 жыл бұрын
I tab them over manually
@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.
@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
@cristianm.310
@cristianm.310 4 жыл бұрын
Very cool stuff. Out of curiosity, what's in the presentGFAlertOnMainThread method?
@seanallen
@seanallen 4 жыл бұрын
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.
@davidlintin
@davidlintin 4 жыл бұрын
I used to pass in a viewController on a network method so I could present an error, this way is much better!
@seanallen
@seanallen 4 жыл бұрын
Happy to help with a new way to do things, David!
@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 жыл бұрын
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 жыл бұрын
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!
@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.
Swift For Beginners - Class & Inheritance Explained
15:42
Sean Allen
Рет қаралды 16 М.
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 8 МЛН
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 51 МЛН
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 56 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 121 М.
Swift Closures Explained
14:23
Sean Allen
Рет қаралды 62 М.
Swift 5 Brand New Result Type: Write Cleaner API code
13:09
Lets Build That App
Рет қаралды 55 М.
Why SwiftUI developers aren't learning UIKit
11:24
codingKo
Рет қаралды 755
Swift Structs - Value Type vs. Reference Type
7:54
Sean Allen
Рет қаралды 15 М.
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 12 МЛН
Мой новый мега монитор!🤯
1:00
Корнеич
Рет қаралды 1,7 МЛН
КРАХ WINDOWS 19 ИЮЛЯ 2024 | ОБЪЯСНЯЕМ
10:04
iPhone socket cleaning #Fixit
0:30
Tamar DB (mt)
Рет қаралды 18 МЛН