Hi. I’m trying to figure out how I can send events from a single thread to trigger an update to my application. I’m a bit confused about the lifetime of a subscription, as described in the documentation.
The documentation specifies that “A Subscription will be kept alive as long as you keep returning it”. But it doesn’t specify under what circumstances the subscription method is called. Does it mean that iced will drop the subscription every frame/update and call the subscription method again? So if I want to use stream::channel for example, I need to create a new channel each frame to replace the old one? Or do I need to store the subscription object and clone it every time the subscription method is called or something, to keep the same channel?
I just want a single channel sender that I can use on a thread to send events to the application for as long as the thread is running.
Warning: I am not that farmiliar with the iced internals so the following might be incorrect. It is only how I understood it.
A Subscription runs as long as we return it from the subscription function.
The runtime calls that method after each update and checks if a new or old subscription is provided.
Every Subscription has an ID. If you use Subscription::run_with_id you specify the ID. If you use Subscription::run the function pointer is used, which could cause bugs, from what I have heard.
If a new subscription is provided, the runtime will start it. If an old one that already runs is provided, nothing happens. If a subscription runs that is not provided by the subscription function, the running subscription is terminated.
Note: Depending if you use 0.13+ or a lower version of iced the subscription function is the function set by Application::subscription or the subcription function of the Application trait
Thanks for the response. So if I understand correctly, Subscription::run will not call the function I pass it more than once even if I return it every update. So in the example in the documentation, some_worker will only be called once, such that only a single Stream is created?