No more [weak self] 🤯 (special video for the 1,000 subscribers)

  Рет қаралды 6,957

Vincent Pradeilles

Vincent Pradeilles

Күн бұрын

Пікірлер
@v_pradeilles
@v_pradeilles 4 жыл бұрын
Did you find this tip useful? Are you planning on using it in your apps? Let me know in the comments 🚀
@StewartLynch
@StewartLynch 3 жыл бұрын
Amazing content. Really, really well planned and presented. Thank you.
@DaveJacobseniOS
@DaveJacobseniOS 4 жыл бұрын
Top notch explanation! I've never seen it done this way but I love it. Congrats on 1k, well deserved and I'm sure many are on the way.
@manivelnagarajan4078
@manivelnagarajan4078 4 жыл бұрын
Good one, Vincent! Thanks for making this video!
@draganmarkovic7221
@draganmarkovic7221 4 жыл бұрын
Hi Vincent, this is a great video. Definitely it’s a must have code in the project 🙏 Keep up the good work!!!
@ioslam
@ioslam 4 жыл бұрын
That’s great , thanks 🤩
@myrickchow3039
@myrickchow3039 3 жыл бұрын
This is a smart idea! Thank you.
@v_pradeilles
@v_pradeilles 3 жыл бұрын
Glad you like it!
@christophescholly
@christophescholly 4 жыл бұрын
Nice trick! Thank you :)
@dev_jeongdaeri
@dev_jeongdaeri 4 жыл бұрын
super super awesome! 😎
@neacor1
@neacor1 Жыл бұрын
By chance is there an equivalent to avoid having to explicitly capture [weak self] each time for Tasks? Here is what I tried to do, however I believe what this does is only give me an optional Self and not a weak reference... protocol TaskExecutor: AnyObject { } extension NSObject: TaskExecutor { } extension TaskExecutor { func executeAsTask(_ executable: @escaping (Self?) async -> ()) { Task { [weak self] in await executable(self) } } } and calling it like... executeAsTask { weakSelf in //async network call that I'm "await"-ing print("foo before getting strongSelf") guard let strongSelf = weakSelf else { return } print("foo after getting strongSelf") //do other logic if this class (i.e. UIKit UIViewController subclass) is still in memory } Edit: To clarify my intent, the goal I was trying to do is have it so if, say, a view controller is dismissed before a service call finishes executing, a strong reference is not retained, and I don't have to deal with (or remember to deal with) "boilerplate" capture of [weak self] each time I want to utilize a Task. Edit 2: I ended up figuring out a solution that works pretty well for me. It still relies on a closure, but at least the logic still flows linearly when you look at the code, so there's that :) Here's what I ended up doing... @discardableResult func executeAsWeakTask(priority: TaskPriority? = nil, _ executable: @escaping () async -> TaskReturnType, processReturnValueWithStrongSelf: @escaping (Self, TaskReturnType) -> ()) -> Task { return Task(priority: priority) { [weak self] in let taskReturnValue = await executable() guard !Task.isCancelled, let strongSelf = self else { return } await MainActor.run { processReturnValueWithStrongSelf(strongSelf, taskReturnValue) } } } and calling it like... executeAsWeakTask { //async network call that I'm "await"-ing } processReturnValueWithStrongSelf: { strongSelf, _ in //do other logic if this class (i.e. UIKit UIViewController subclass) is still in memory }
@Vigotskij
@Vigotskij Жыл бұрын
We can capture the task in a private variable i.e.: ```swift var tasks: [Task] = [] // code func prepareForDeinit() { for task in tasks { task.cancel() } tasks = [] } ``` edit: this way we don't need to care for capturing self as weak, as well as we call prepareForDeinit in the proper moment
@vaibhavindalkar707
@vaibhavindalkar707 4 жыл бұрын
Cool trick. There is blank screen from 12.02 to 12.07. Is it some kind of glitch or intentional ?
@v_pradeilles
@v_pradeilles 4 жыл бұрын
Damn it's a mistake in Final Cut on my part. Hopefully it doesn't seem to hurt the explanation to much.
@vaibhavindalkar707
@vaibhavindalkar707 4 жыл бұрын
@@v_pradeilles No noting wrong with your explanation. While watching it suddenly stopped so wondered. Your videos are like tweets on KZbin short and sweet. Keep them coming.
@its-kapucha
@its-kapucha 2 жыл бұрын
What if self will be deallocated and we want to execute some required business logic that have reference to self in completion closure?
@skanderbahri5274
@skanderbahri5274 3 жыл бұрын
Amazing content from now no more weak self in my code
@jamilurrehmanamini4878
@jamilurrehmanamini4878 3 жыл бұрын
Very helpful trick
@ericwilliams4554
@ericwilliams4554 Жыл бұрын
cool. thanks.
@vinayhosamanenijaguna
@vinayhosamanenijaguna 3 жыл бұрын
This is cool !
@swiftAI
@swiftAI 3 жыл бұрын
Can just using [unowned self] work the same way? How does it compare to your solution?
@v_pradeilles
@v_pradeilles 3 жыл бұрын
You could indeed use the same approach for [unowned self]. However, since unowned doesn't require the extra syntax to then unwrap self, it wouldn't provide any real benefit.
@GoeHybrid
@GoeHybrid 9 ай бұрын
Can we just extract closure into a method and remove the guard statement?
@emrahkorkmaz3991
@emrahkorkmaz3991 3 жыл бұрын
hey Vincent, thanks for the cool tip, I would like to ask something. What is the role of the Self in a callback? Call parameter block has one parameter (T) -> Void but Weakify has 2 input values as (Self, T) -> Void. I assume that we give 2 parameter callback to 1 parameter callback and it works.
@v_pradeilles
@v_pradeilles 3 жыл бұрын
That's indeed the explanation: the function weakify makes the link between the 1 parameter callback and the 2 parameter callback 👍
@benaounimed1557
@benaounimed1557 2 жыл бұрын
waw waw waw waw. C'est génial!!!
@Karthik-gr7ie
@Karthik-gr7ie 4 жыл бұрын
Awsome.
@indiekiduk
@indiekiduk 4 жыл бұрын
I thought you were gonna say make the format method static and use a local var for the label
@v_pradeilles
@v_pradeilles 4 жыл бұрын
In the context of the small portion of code I used as an example, making the method static would indeed have been a (much) better solution! However, when you scale things to a larger project, where components have several dependencies, it often becomes much harder to extract behavior into static functions. For instance, if you follow MVVM, imagine a controller that would be calling a method on its view model.
@mrdrip4897
@mrdrip4897 4 жыл бұрын
u lookin fr an editorr
@narjesabbaspour4695
@narjesabbaspour4695 3 жыл бұрын
thanks. just thinking, if the Iboutlet is declared as weak can retain cycle still happen? (without weak in closure)🤔
@rasinsahaji5296
@rasinsahaji5296 Жыл бұрын
What if my clousr has more than one value? how to handle that case, can anyone help?
@v_pradeilles
@v_pradeilles Жыл бұрын
Then you can define overloads of `weakify` that take more than one argument. You can find the code here: github.com/vincent-pradeilles/weakable-self
@気にしない-o8q
@気にしない-o8q 4 жыл бұрын
wow.
@Maukurzo
@Maukurzo Жыл бұрын
It's looks amazing, but debug will became strong
@roman3249
@roman3249 3 жыл бұрын
In the resulting code can we use name "self" instead of name "strongSelf"? Just curious 🙂
@v_pradeilles
@v_pradeilles 3 жыл бұрын
Technically you can, but you’ll probably have to escape it using back quotes. So the ergonomics are not that great 😕
@IleshPanchalOnly
@IleshPanchalOnly 3 жыл бұрын
I’m not using it, let me try and get back to you. 😋
@douglasfers7
@douglasfers7 3 жыл бұрын
Nice video! But you need to improve your conversation skills 😂 sometimes it is very hard to understand
@Antonnel7
@Antonnel7 2 жыл бұрын
This no good tip. Good code no leak, mini code: service.call{ [format,label] in //Logic here //end }
Experimenting with async & await in Swift
15:50
Vincent Pradeilles
Рет қаралды 10 М.
Swift Tips #30 - weak & unowned
8:15
Vincent Pradeilles
Рет қаралды 4,2 М.
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54
When do we REALLY need to use [weak self]? 🤔
7:30
Vincent Pradeilles
Рет қаралды 8 М.
Swift Tips #31 - some (Opaque Return Types)
8:02
Vincent Pradeilles
Рет қаралды 5 М.
MVVM vs MVP: what's the difference? 🤔
9:27
Vincent Pradeilles
Рет қаралды 10 М.
How to use weak self in Swift | Continued Learning #18
20:33
Swiftful Thinking
Рет қаралды 19 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 26 М.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 57 М.
How to implement Dependency Injection in Swift!
12:56
Vincent Pradeilles
Рет қаралды 13 М.
When is it RISKY to use [unowned self] inside a closure? 🤨
7:31
Vincent Pradeilles
Рет қаралды 1,9 М.
Actor Reentrancy in Swift explained
20:18
donny wals
Рет қаралды 3,5 М.
Strong vs. Weak Swift 5: What is Weak Self (Xcode 11, 2020)
16:41