Ancestors

Written by Rhys Morgan on 2024-08-30 at 13:35

@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

Written by Gernot on 2024-08-30 at 14:19

@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

Written by Rhys Morgan on 2024-08-30 at 14:42

@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)

=> View attached media

=> More informations about this toot | More toots from rhysmorgan@mastodon.social

Written by Rhys Morgan on 2024-08-30 at 14:50

@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 😅

=> View attached media

=> More informations about this toot | More toots from rhysmorgan@mastodon.social

Written by Gernot on 2024-08-30 at 14:56

@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

Toot

Written by Gernot on 2024-08-30 at 14:56

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

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113051552268156322
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
288.669669 milliseconds
Gemini-to-HTML Time
1.157694 milliseconds

This content has been proxied by September (3851b).