In my application I create page and after the page is rendered I want to perform non-blocking, but not async action.
Similar like useEffect
in react with empty dependencies.
For running a piece of code non-blocking you need to use a Command/Task(both the same thing → it got renamed between iced versions). You can browse the examples in the iced repo or take a look at this part of my guide.
If you have non async code you can take a look here how that can be done.
Unfortunately, guide from Blocking Code - Unofficial Iced Guide is not working for me, since I need to call method on struct, that can not be shared between threads.
And Task(Command) should be called from outer scope of page, but what I really need is to call it from page within, but only once and after first render.
Have you tried to wrap it in a std::sync::Arc<tokio::sync::Mutex<YourType>>
? That helped me when I had such problems.
Wrapped in those you can simple clone it and lock().await
or lock().try_lock()
it in your command/task.
Yes, in that way it works.