The power of Combine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

😍😍 Loved it. For someone coming from clojure and thread macros, this really awesome. Thanks for sharing

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/tami5 πŸ“…οΈŽ︎ May 08 2021 πŸ—«︎ replies

Loved your tutorial, i have queries: 1- Can we use Combine without SwiftUI, (asking this question because the legacy project I am working on since ages, doesn’t have SwiftUI nor am I planning to rewrite it in SwiftUI) but i would love to integrate Combine if its available w/o SwiftUI. 2- I really need to subscribe you on Youtube, can i have your channel link?

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/Accomplished_Ad_1740 πŸ“…οΈŽ︎ May 08 2021 πŸ—«︎ replies
Captions
hello guys welcome to another video this is a video that you chose to watch first so thank you so much for voting on twitter and remember follow me on swift tips if you want to decide which video we'd like to see next anyway today we will talk about combined two years ago combine was introduced along with with ui to create a new declarative api to process data through a flow of asynchronous events but the question is why why we need to learn combined to start building applications with it and what is advantage over imperatives apis that we already have well take me an introduction to combine and show you the power of combine in a really quick example and we will cover a series of videos about all the things that you need to know about combine but for now let's get started my name is pete and this this is fun tips [Music] nowadays we have different apis to build applications for apple platforms like ios or mac os apis and tools like notification center grand central dispatch operations timers url session core data callbacks delegates etc etc we have been using all of them for years however at some point apps became harder to build because well today we have better specs in the devices to make fancier stuff but also because we add more and more features to our apps with apis that work pretty different each other that's why combine entered in action in 2019 along with swift ui for the case of tools vi it's easy to see what is the goal keep building awesome ui with less volatile code and also centralize the source of through for or apps it's more or less the same for combined but oriented to data we are looking for a way to centralize our business logic through a flow of asynchronous events one after other in that way it's easier to see what should happens before and after an event in words of apple combine is a declarative api which means we have clear statement about what is happening with the outcome let's see an example let's see this imperative code that is explicitly saying what would happen here we have a loop and we have a condition but now look the declarative way it's pretty steep forward to see that this array is filtered by a condition so yeah essentially is the same but the context is really straightforward to follow combine is also applying something called functional reactive program or frp for short combine is not the first framework using this paradigm for many years dev have used rx swift or reactive swift to reach that however combine is a first body framework which means apple give full support for that and also is well integrated with swift ui combined made use of three concepts publishers subscribers and operators let me explain these concepts and how is the flow using combine you pay a company to receive a drinking water service the water goes through different filters and quality process through pipes so that you can drink it and finally you can open your faucet and receive that water that will fall into your sink wingwink if we transform this into a combined environment we have this we have the publisher in this case that is the company providing the service the subscriber that is paying the service and receiving the water and in the middle we have operators that filter the water and do more operations during the flow all this is called has a subscription and the water here is the data that we pass and transform from the publisher into the subscriber instead of deprecating the legacy apis apple adapted to work with combine you can use combine for working notifications timers ui bands decoding and encoding etc etc this is just the beginning combine is a really powerful framework and we will explore in details all the features and components to make awesome apps with combine i would like to show you one example and illustrate you the power of combined this is a swift ui demo let me run this before explaining the code this demo is really simple it's just a pokemon list with 151 first pokemon so yeah are the ones that are from my generation i know i'm old we have a button to reload there's no detailed screen or anything because the point of this example is to show you what is inside of this particular business logic before starting just let me remind you that if you have problems understanding optimal objects or how things work with swift ui i already created a series of videos about the state management and an intro to stacks i will leave in the description all that information for you if it's required anyway so as you already saw we have a list and we have a pokemon cell that it displaying the pokemon information let me just show you what is inside of pokemon this is our model we have a struct that is a pokemon response because we are making a call to an api you will see that in a moment and we are transforming our pokemon response into a pokemon object that we use in our list so this is the one that we are displaying in the screen all right the important thing here is pokemon loader let's go back here and this is the business logic of our application first we have an enum we have errors for server error and no data and an id that i will explain you in a moment and now we have a class pokemon loader that is an observable object and if you already notice observable object is a protocol from combined framework this is awesome and this is just a sneak peek that swift ui is heavily using combined under the hood to all the operations for the state management let me go back for a moment and we have also published property wrapper that is wrapping every property here and publish is converting all these values into publishers yeah do you know that all these values are being providing data in this case pokemon data is a list of pokemons and these two are blacks are booleans so this means someone that subscribed to these publishers will receive this data in this case we can see that we decorate pokemon loader with observed object which means this view pokemon list is subscribe to pokemon loader that's the way internally we are using combiner under the hood already in the videos when we talk about state management so it's pretty nice but let's see more we have other couple of things here can sellables that we will talk more in details in a future video but it's basically an object that is saving a token for the system in order to eventually you know cancel and delete the information about the publisher because this is this is useful for the memory management of each subscription and the rest is well known url session you know already that is the first party framework to make api calls or everything related to networking and we have a url we are connecting in this example to poke api it's a free api that you can use for your projects if you want you know to just test something or if you want to build a pokedex let me go back again to pokemon list and let's see something really interesting here every time this view is appearing we are calling pokemon loader.low that's nice but let's see what is inside of this so here we have the magic of this application we have first any state of is loading that we call every time we started this because we are telling the user that we will start loading something and we reflect that as you can see we have a c stack which means that this value will be at the top of the screen when we are loading something so here again we have the cancelable object but it's basically holding the publisher data so in this case we have euro session and look at that this is the magic of combine we are chaining many operations one after other at the end we receive the information in this closure receive value and then we assign pokemon data to the final response but what is happening here first we execute data tasks but in this case it's a data tax publisher so in other words it's basically doing the same data tasks that you already know has a closure but in this case it's exposing a publisher it's doing the same but it's returning a publisher then we use dry map it's a operator in combine to you know try to map something otherwise we'll return an error in this case we try to validate if we got a response a valid response otherwise we'll return a pokemon error.server error which means something happens and we won't continue but if everything is okay this return value will be the output data okay cool so finally with a code or value already talk about pokemon response so yeah we're trying to decode the data into type pokemon response with a decoder json decoder once you recall you map this value using a in this case a keypad we will also talk about keypads pretty soon but basically what we are saying here is that we don't want the whole pokemon response we want just result and if we go back here we already talked that we won just resolved alissa pokemon which is this struck and finally we do this operation with map what is doing this well it's just adding the id or you know the pokemon number to the pokemon object because by default this api is not providing that number so we are generating that by ourselves and will be useful for the images in the application if we go back here every image here contains a prefix of every pokemon number that's why we are using that anyway once we finish with all of that then what happens we need to receive more data on main thread so we are calling this specique dot main which is the reference to use main thread because the thing that we will use in the screen well requires always the ui to be in main thread and if you remember your session works in a background thread that's why this step is really important if you don't provide this well you will see memory issues in your application anyway after all these operations we finally got sync and in this case sync is the subscriber of this publisher because it's receiving the information everything that is receiving the information provided by the publisher is called subscriber and sync is one of the two built-in subscribers that combine is providing we will talk more about subscribers pretty soon too anyway here we have just two closures we have one closure to receive the value so after all the processing yeah we just got pokemon data from the response and here we receive the completion closure by saying okay whatever happens the loading is finished and then we check what is the completion state in case of finish this means that everything is fine so no error and we do nothing but otherwise if we got a failure here we in this case print the error and then we say that there is an error error equals true and that's it as you can see it's really easy to me to explain you every step because this is the magic of combine we can identify really easy what is the first operation what is the second one etcetera so on and then finally what we'll receive but look at that compare that with the traditional way i know you can do this in many ways but let me show you this with the closure approach you have here url session with data tasks but look you need to validate every step individually and as you can see i don't know you but this is for me hard to manage i know you can say hey but you can create also functions to do the same and try to keep it simple but with the power combined every operator here is returning a new publisher a new value containing the transformation of our data for free it's awesome to see that and also as you can see this is the default way to work with swift ui because at this point we are just saying pokemon data equals response and that's it because pokemon data is exposing to the subscriber this information that's it i don't have to do anything else however here we need finally to send the completion handler to someone else and then expect this completion handler will be called in uh main thread because we didn't set that we also need to think about if we need to call this dispatch queue to maintread in this piece of code or in some other place yeah there are a couple of things that in this example could be really easy to solve but believe me if you have a big project or you're working in a company it's really hard to try to maintain what is a source of truth what is or are the right process to complete all these operations but with combine you can reach that really easy and yeah we will talk more about every piece of this process this is the power of combined that's it for this video we have a lot to cover for combined but believe me combine is really powerful it's a really great api for you to build applications and centralize your data your process your events we'll cover more about it and also related to swift specifically things like keypads never type closures etc but i will leave it up to you which video we'd like to see next please let me know in the comments and don't forget to follow me again on twitter swift and tips because i will publish a couple of polls about what is the next video that you want to see that's it for me thank you so much and have a great day you
Info
Channel: Swift and Tips
Views: 1,243
Rating: undefined out of 5
Keywords: swift and tips, swift && tips, swiftandtips, pitt500, Pitt, Pedro Rojas, Combine, Swift, SwiftUI, Pokedex, functional reactive programming, What is Combine?, publishers, subscribers, operators, map, decode, pokemon, urlsession, ios, ios development, Combine Framework
Id: IJinuMLaERE
Channel Id: undefined
Length: 15min 48sec (948 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.