@mattiem Hey! Do you have any guidance, documentation, resources you find useful on when to (and when not to) do [weak self]
inside a Task
?
I know that it does retain a strong reference to self, but then I see recommendations that just retaining a reference to the Task
and calling task.cancel()
gets around that. I see some suggesting adding it in your model/view model/whatever's deinit, but that deinit would never be called if a Task has a strong reference, …right? 🤯
=> More informations about this toot | More toots from rhysmorgan@mastodon.social
@rhysmorgan @mattiem Also if self is a struct, like most things in SwiftUI, there is no retaining, and thus no retain cycles.
=> More informations about this toot | More toots from gernot@mas.to
@gernot @mattiem Yep, that stuff makes a lot of sense to me. I think the compiler stops you even weakifying self in that use case anyway, right?
I'm trying to figure out if there's a "better" solution to something like this, using Task cancellation, or just to stop trying to be so "smart" 😄
(quotes, because I realise that this code probably works just fine and just let it be, and messing with it might not actually be so smart)
=> More informations about this toot | More toots from rhysmorgan@mastodon.social
@gernot @mattiem I've come up with this as an alternative, but also aware that it now needs cancelling from the outside to avoid any chance of a "retain cycle”, or pushing the Task
creation out to the View Controller layer, which makes the “always use currently active Task" thing not really testable 😅
=> More informations about this toot | More toots from rhysmorgan@mastodon.social
@rhysmorgan @mattiem Anybody correct me if I’m wrong, but the former version had the same functionality. You have to call it within a Task anyways, and if that is canceled, the Childs are canceled. A Task{} that is not in a parent taks has to be canceled manually, if it is in a parent task, it will be canceled like any other child. Task.detached{} has no parent. (1/2)
=> More informations about this toot | More toots from gernot@mas.to
And a .task modifier is canceled when the view disappears, so if the request is only relevant to the view, use that. (2/2)
=> More informations about this toot | More toots from gernot@mas.to
text/gemini
This content has been proxied by September (3851b).