Actors in Swift iOS || Main Actor || Isolated Actor || Non Isolated

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to this video in this video we are going to learn about the actors in shift this actors concept is already implemented in other languages like python java long days ago but shift has introduced it recently to overcome the data race concept this shift has been introduced the actors in ios 15. so let's see what is data rays first so so for that i am just going to open an xcode i am just going to create an object so ok so lets consider there is a refrigerator in your home and there is an access for the everyone in your room that they can go and take their desire items from the refrigerator so if you are going and taking an items from refrigerator and in same refrigerator if some other go and see means that particular item will not be there because it has been already taken from taken by you from the refrigerator so but when we are doing it in concurrency way like this will this will go and go into the process of following the data race so let's see what it is so for that i am just going to create a class class in the name of refrigerator so this is going to be have an variable array items which is of type string so this is going to have milk drinks vegetables come on chicken i like chicken very much so chicken okay so this is some of the ra items that this that particular refrigerator is uh having so i'm going to create another function which i'm going to get all the list from the all the items that is presented in the array get items from fridge refrig so yeah just i'm just going to return array items okay so in the written case i'm just going and having like array of strings that's so cool i'm just going to create another function to get any items from the array so for that get item okay so i just created another item this is going to return a string so first i'm just going to get and run value to retrieve any items from the array so this is going to have type in dot random value which is going to take from 0 to array items here we go so then i'm just going to get an object that is presented inside the nra items so i just get in the rand value okay next thing i'm just going to remove that particular items from the rand value yes we go and i'm just going to return this particular value that has been taken here we go so i just written over that so here i can get take like get item get details instead of items so that will be appropriate manner okay so so i just created a class which is warnings an array of items which contains refrigerator items like milk drinks vegetables and etc so in next thing i have created an items where i have just returning all the items that is presented inside center refrigerator and next comes to like i just get take and ran value of index and i am just retrieving that particular value and removing that particular items from the refrigerator and returning that particular item so here we go it's very simple so in in i'm just i returned this in inside the view controller so i'm just creating going to create an object instance for refrigerator of this is going to be equal to refrigerator and i'm just going to allocate the value for this and here i'm just going to return array of take one item and object dot take get details from the refrigerator so first it will retrieve some value from first it will retrieve all the array from retro any one of the item and after that it will return the items that is presented inside uh the things after executing the value but here it's very simple you can get but when we are going for a concurrency manner this going to be create and data arrays so let's create that so for that let me go and create an dispatch queue dispatch group dot global dot async so here i'm just going to print taken is so i'm just going to print this yeah here we go and next i'm just going to be in another group and i'm just going to print it over here okay remaining item here we go it's okay so i just uh going to take one item from the fridge and after that i'm just going to retrieve all the items that is presented inside so i'm just going to create it's asking me to self and please remember when you are doing you don't create a strong reference this may affect for testing purpose i am just using the self so i'm just going to run this and let's see what's output okay so here you see like the item taken is milk and the remaining item is milk drink vegetable and chicken you see there's a race condition so this particular thing is called rice condition because i have already taken a milk but still the fridge is saying me like it has a milk element in it so i'm just going to run another time and let's see what's the output is okay this time it's taking a drink but still now the fridge is saying me like there is some drink available in the fridge so this is totally wrong so this particular cases is called race condition here we see the race condition so to overcome this race condition previously traditionally we have used like barrier dispatch queue to resolve this problem but that are more difficult to understand and there can be a error because those are manually interpreted to overcome this particular issue shift has introduced an actors to resolve to resolve this let's go and fix with the help of factors so for that it's very simple so instead of this class we are just making this particular object this particular instant as an actor and when we try to run this so this shakes a compiler like actor is a method that can be isolated referenced of the main actor so you see like whenever a particular object is named under the category of actor then the compiler will think that it has been an concurrent process and so it has makes the compiler to tells like dasbin isolated property so when we says an isolated property so that the com when multiple threads was try to occur the same instance or same uh shared property so it will makes one at a time so it will make only one instance to ah access the objects and rest of the instance will be weight and once the particular instant particular instance or thread is completed the other thread will taken over the object so this is how it has been designed so for that what we need to do is like to resolve this issue we need to create an keyword called weight i have already created a video for how this await is working in ios so you can see around in this i will keep an er description in the below okay so this particular things has to be taken under care of task instead of dispatch async so i'm just going to create a task for it so in the task i'm just going to print it so let me remove this and i'm just going to create another task and just i'm just going to remaining the things okay so i just created a two task now you see like you can you can issue a result now if i run the build let me run the build and this time you can see like okay i have just missed the minus one okay yes so this time when i when i try to run this object you can see like the item is taken is drink and the remaining item that is presented in milk vegetable and chicken so this states like so whenever a concurrency process that has been occurred on a shared instance still can get to retrieve the value very easily so to to it's it's it's very simple just making an class to an actor it's very simple but there are some difference between some class and actor that you have to see is like they are more or less same but here inheritance is possible where in class and where an actor the inheritance is not possible and it support concurrent accessing and class is not allows the concurrent accessing so with help factor you can achieve this output very easily and it's very clean now next thing is like i'm just going to have an objects that is that is lit uh let's take it as welcome message so this is going to of typing hello actor so i'm just i'm going to try achieve this value retrieve this value as instance and if i if i run this if i if i try to retrieve the milk welcome message it says like async welcome is used in a contact and does not support concurrency because when we created this welcome message this is of typelet so this intimates like this value cannot be changed so this cannot there is no reason for having an actors because actors are usually a variable because then only a value can be accessed by different kinds of instance or different kinds of thread so for that we can tells a compiler like non-isolated keyword so by this keyword this compa this knows like the particular object is not be an access for any other thread so it cannot be changed so when we once we have given this when we try to access this welcome now welcome message now this time compiler won't give any uh error or any intimidation in the warning so this is this is what uh non-isolated things and there is somewhat important thing that we need to see in actor is like so when we are going for a dispatch group and when we are going for shifting from background thread to main thread and at that time we will be making and labels or we will be making an ui setups in and main thread only the reason why we are doing an main thread operations we are doing an ua operation in the main thread dislike that will be a more complex one so that compiler will get given i priority for that and when plotting of the pixels it will be more uh comp av complex so that always when we are doing some ui related works we always do in a main thread but now a shift has introduced an another concept like main actors so for that i am just creating the word update ui so this update ui so i have already have a label in my view controller so this is going to of type main actor okay so like consider when we are going for an uh updating a ui we used to use dispatch q dot dispatch q dot main dot async so on value is being called and you used to call this self dot update ui but instead of doing this we can resolve instead of doing this we can make this particular function as yes so this tells a compiler like this particular uh object is going to of type main thread so whenever this uh whenever they're calling this update ui function this is going to be called the main thread only so instead of having every time in a dispatch to queue.main.tasking code we can just make that particular function as main actor and it will get updated the labels in the main thread only so this is one of the other important thing that we need to focus so you see like you can i can see the label here so i have already have in um main dot file i already have a label so that label is printed out here so let's summarize what handle i have until we have learned today so first thing is like actor so the actors are used to uh avoid and dead race conditions when when concurrent operations were done to the single uh a shared instance so for that only actors were used actors was introduced in the in ios 15 and the replacement of the actor that we have used traditionally is like a barrier cue but using of barrier cue that think like that isn't some error may be occurred because those are manually interpreted and like we have one concept like a non-isolated one those values will not be changed or considered as a non non isolated one and values that has been shared instance are considered as an iso atomic one and next comes to the actor versus classes so actors have an um concurrency in a concurrency task but uh class doesn't support that actors doesn't have an inheritance but class have an inheritance and to updates and ui and update and everything in the main thread we have introduced a new concept like main actors that we have to follow so that's all about this video and this is all about the basic informations of the actors and i also recommend you to watch another video which i have created for async weight and if you see that video it should be more clear for you and i hope like you have enjoyed this video and thanks for watching this video and see you in next video bye you
Info
Channel: Mohan Raj
Views: 92
Rating: undefined out of 5
Keywords:
Id: O5THBr-xBBQ
Channel Id: undefined
Length: 16min 43sec (1003 seconds)
Published: Mon Oct 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.