Strong vs. Weak Swift 5: What is Weak Self (Xcode 11, 2020)

  Переглядів 23,728

iOS Academy

iOS Academy

День тому

In this video we will learn about memory management in Swift 5 and Xcode 11. Specifically we will look at strong vs weak memory retention and how it relates to ARC (automatic reference counting)
💻 Source Code: / iosacademy
🎥 Subscribe for more: ukposts.info?su...
😎 Like my teaching style? Check out some of my most popular courses! courses.iosacademy.io
#swift #iOSDeveloper #swiftdeveloper
Join the iOS Academy Community: iosacademy.io/
** Get Skillshare free for 2 Months and learn iOS
www.skillshare.com/r/user/afraz
** Manage all your investments from app earnings on Betterment!
bit.ly/3eBwlI9
** Grow your own UKposts tech channel with TubeBuddy:
www.tubebuddy.com/iosacademy

КОМЕНТАРІ: 69
@hykim5137
@hykim5137 3 роки тому
Awesome! I was really looking forward to a video on this topic from you. Thank you for making it.
@iOSAcademy
@iOSAcademy 3 роки тому
Youre welcome!
@wannabemusician5519
@wannabemusician5519 3 роки тому
omg Thank you so much for explaining this out.! i love ur content btw. :)
@iOSAcademy
@iOSAcademy 3 роки тому
Youre welcome!
@Ali-uh5th
@Ali-uh5th 3 роки тому
Great! The explanation is so clear and easy to understand. But it seems that the audio volume is a quite low. Keep up the good work!
@iOSAcademy
@iOSAcademy 3 роки тому
Thank you!
@dalton4842
@dalton4842 3 роки тому
Clearest explanation out there for this :)
@iOSAcademy
@iOSAcademy 3 роки тому
Thanks! Did you like my balloon example hahaha
@aafaqahmed2265
@aafaqahmed2265 2 роки тому
It was awesome, best explanation ever scene on this topic!
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks!
@amielterence
@amielterence 2 роки тому
Thanks bro, really helped a lot👌🏽
@iOSAcademy
@iOSAcademy 2 роки тому
Glad it helped
@hustlas4ever
@hustlas4ever 3 роки тому
wow! was reading about this the other day and can't really understand. lol. Thank you for this.
@iOSAcademy
@iOSAcademy 3 роки тому
Ha ha you’re welcome, glad I could help. Don’t forget to subscribe!
@rahuljamba5846
@rahuljamba5846 Рік тому
Very unique way to define weak and strong also handle memory leakage.
@vuralcelik8060
@vuralcelik8060 3 роки тому
Great explanation !
@iOSAcademy
@iOSAcademy 3 роки тому
Thanks!
@RahulGupta-wp6bk
@RahulGupta-wp6bk 3 роки тому
Very Helpful video...Thanks Bro.. :)
@iOSAcademy
@iOSAcademy 3 роки тому
Your welcomd! Dont forget to subscribe and like
@dev_jeongdaeri
@dev_jeongdaeri 3 роки тому
Super awesome!
@iOSAcademy
@iOSAcademy 3 роки тому
Thanks
@radiagulzan
@radiagulzan 3 роки тому
thank you !
@iOSAcademy
@iOSAcademy 3 роки тому
Youre welcome
@jiayi4154
@jiayi4154 3 роки тому
Thank you very much for the content! Can you do one for core data and saving and retrieving in an ios file system?
@iOSAcademy
@iOSAcademy 3 роки тому
Youre welcome & sure! Added to my list
@user-yd9xy3rb4x
@user-yd9xy3rb4x 2 роки тому
Not even a month ago, did I think the topic is incredibly hard to understand. It's a piece of cake now.
@iOSAcademy
@iOSAcademy 2 роки тому
Glad to hear it
@hasanbasriozturk1550
@hasanbasriozturk1550 2 роки тому
Still the best
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks!
@araapoyan2814
@araapoyan2814 3 роки тому
You are the best
@iOSAcademy
@iOSAcademy 3 роки тому
haha thanks!
@chatsnoirblamo
@chatsnoirblamo 3 роки тому
Thank you because I learned Java in Eclipse and it was obvious we were pretending memory didn’t exist. That’s fine for python scripts but I want to move into real apps!
@iOSAcademy
@iOSAcademy 3 роки тому
Youre welcome! Glad i could help
@rudolphhock1155
@rudolphhock1155 3 роки тому
It doesn't seem that iOS is going to pick up on memory leaks. I haven't delved into this subject much more than this video, but surely Swift should have built in tools to help pinpoint memory leaks ?
@dodilodi1278
@dodilodi1278 3 роки тому
12:00 It would be the same even if func getData() wasnt private?
@alamgirkhan001
@alamgirkhan001 Рік тому
Great explanation! Make a video on Local DB in IOS App with SQLite.
@iOSAcademy
@iOSAcademy Рік тому
Will do soon
@kuldeep4954
@kuldeep4954 2 роки тому
Please do a follow up video
@iOSAcademy
@iOSAcademy 2 роки тому
Coming soon
@ievgenievgen3651
@ievgenievgen3651 2 роки тому
You give wrong examples about reference cycles. In all your examples, you use a local reference type variable of functions. Such variables do not hold by class it is in the function scope. If you move your alert var in class scope, then you will get ref cycle. You can easily check that by using the deinit in your class for checking did you viewcontroller call it or not.
@nohbodi
@nohbodi 2 місяці тому
Was waiting for someone to point this out. Glad to know I'm not going crazy.
@boxinggio
@boxinggio 3 роки тому
In SomeDelegate example you wrote weak var delegate : SomeDelegate? is weak necessary? I think making it optional is already weak right?
@iOSAcademy
@iOSAcademy 3 роки тому
Weak is needed to avoid retain cycle.
@boxinggio
@boxinggio 3 роки тому
iOS Academy but optional doesn't already have weak reference?
@faastfood
@faastfood 3 роки тому
I tried AlertConroller example and checked in instruments but didn't get leaks indicator. Can you please tell me why is it happening?
@puneetmahali1647
@puneetmahali1647 3 роки тому
Yes, I have also some confusion, as video tutorial states that the code doesn’t give any error but if give weak self then it will not leak the memory. Then how do we know when n where memory will leak?
@choonshon5165
@choonshon5165 3 роки тому
Tell me if I’m wrong.. but I think it’s because the AlertController is not owned by the “ViewController”. Once dismissed, the AlertController has no reference pointing it and it is released.
@ningzedai9052
@ningzedai9052 2 роки тому
The answer is simple, what this youtuber said is not correct about the alert controller. The alert controller is not owned by "self", so there is no retain cycle. He is misleading others.
@TephrosXI
@TephrosXI Рік тому
What if the "self" in a closure was of Struct type; Is it still possible to have a retain cycle?
@neslzkusfep
@neslzkusfep Рік тому
Hi westo127! My response may not be the most precise or accurate as I am also still learning, but this is my understanding of iOS memory management. To address your question, the object's data type (i.e. Struct) isn't the concern. A retain cycle occurs when two separate objects are strongly pointing to (referencing) each other; if one of the objects is deallocated (removed) from memory while another object is still strongly referencing it, the object that is referencing the deallocated object will still exist in memory somewhere, pointing to (or otherwise linked to) some object that no longer exists in memory - taking up space while serving no purpose. The goal of proper memory management is to ensure that objects that are added to memory are deallocated if they are no longer being used. In the example provided, there are two objects: ViewController and Alert. The parent ViewController object is strongly pointing to a child Alert object because the Alert object is defined within the ViewController object; this Alert object is a separate object from the ViewController object though it is defined in the ViewController class. If memory is properly managed, the Alert object should be deallocated from memory if the ViewController gets deallocated from memory. Before the edit was made to the Alert object's handler, the Alert object was strongly referencing the parent ViewController class by calling the parent ViewController's method 'doSomething()'. Since the ViewController object strongly references the Alert object and the Alert object is strongly referencing the ViewController by calling the ViewControllers doSomething() method, this causes a retain cycle. To prevent a retain cycle, you want the child Alert object to weakly reference the ViewController's doSomething method; that way when the user dismisses the Alert object from the View - an action that should deallocate the Alert object from memory - the strong reference to the ViewController won't prevent that from happening. Hope this was helpful and not just a reiteration of what was previously discussed in the video. :)
@sharbel9624
@sharbel9624 3 роки тому
For some reason I'm having a hard time replicating this on swiftUI rather than UIKit. Any chance anyone can help me out here?
@RatherBeCancelledThanHandled
@RatherBeCancelledThanHandled 3 роки тому
Thanks for sharing but it seems to me like there is no point in using a strong reverence . Why wouldn’t we use a weak reference ?
@TheBooban
@TheBooban 2 роки тому
Strong is default because you don’t want your object to just disappear when you need it, do you? So figure out how you want to handle it, and then use weak.
@Shermyyy
@Shermyyy 3 роки тому
#suddenlyitalian completino hahaha but great video! very informative!!
@Chromasophy
@Chromasophy Рік тому
self? as an optional makes sense ~
@iOSAcademy
@iOSAcademy Рік тому
Agreed
@jason18401
@jason18401 3 роки тому
So should use [weak self] in all closures?
@iOSAcademy
@iOSAcademy 3 роки тому
Yes, almost 99 percent of time
@pratikgajbhiye5354
@pratikgajbhiye5354 3 роки тому
One small question, when to use self and when not?
@iOSAcademy
@iOSAcademy 3 роки тому
Swlf is needed in closures
@aviadsabag8193
@aviadsabag8193 3 роки тому
When you want to ref to your current variable inside VC, for example in init u can assign what u get as a param into your local variable. for exmaple - self.count = count(the param that u get from the user). btw there's self in every programming langue :)
@intros1854
@intros1854 2 роки тому
I feel pretty embarrassed 🤭. I made whole project with memory leaks 😂. Thanks, I'm gonna fix it right now
@iOSAcademy
@iOSAcademy 2 роки тому
We’ve all been there!
@user-xx7hp8jy2x
@user-xx7hp8jy2x 3 роки тому
weak selfムズすぎる😭
@iOSAcademy
@iOSAcademy 3 роки тому
Yep haha
@AkimboFennec
@AkimboFennec Рік тому
Not a good explanation. Did not understand it at all.
@iOSAcademy
@iOSAcademy Рік тому
Thanks for the feedback
@vikramt87
@vikramt87 2 роки тому
very very complex.. please dont upload this kind of videos create more confusion
Swift Retain Cycle / Memory Leak Explained (iOS Interview Question)
9:05
Logan Koshenka
Переглядів 3,2 тис.
Этого От Него Никто Не Ожидал 😂
00:19
Глеб Рандалайнен
Переглядів 4,2 млн
Зомби Апокалипсис  часть 1 🤯#shorts
00:29
INNA SERG
Переглядів 3,8 млн
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Переглядів 55 тис.
When do we REALLY need to use [weak self]? 🤔
7:30
Vincent Pradeilles
Переглядів 7 тис.
self vs Self in Swift  || iOS interview Questions
11:29
App Developer
Переглядів 3,3 тис.
5 Dangerous Things to Avoid Saying In a Job Interview
12:57
Don Georgevich
Переглядів 6 млн
Prefetching with TableViews (2022) - iOS
16:35
iOS Academy
Переглядів 18 тис.
How to manage strong & weak references with Async Await  | Swift Concurrency #13
16:27
Threads and Grand Central Dispatch in Swift
22:14
Stewart Lynch
Переглядів 20 тис.
Этого От Него Никто Не Ожидал 😂
00:19
Глеб Рандалайнен
Переглядів 4,2 млн