-
Dear community, My goal is to run three async Void functions sequentially. In the snippet below, the intention is to clear the current documents, print the new documents, and call the completed action.
In the snipper however the print completed is fired before the print is done. I assume this is because the functions are not run sequentially and in the time print is running, printcomplete is already called. I was wondering if there is a TCA idiomatic way to do this and would highly appreciate your input. Thank you in advance. PS: it is my assumption that the provided code is enough information for an answer, but I'm more than happy to share more code if it is helpful to you. PPS: I have also tried the following, with the same results:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @tenthijeboonkkamp, the You can even confirm this by annotating the closure with return .run { @MainActor in send in
send(.print(.clearDocuments))
send(.print(.print))
send(.printCompleted)
} This shows that the 3 actions are sent one after another synchronously, regardless of what logic and effects are being executed for each action. Is there a reason you have separated all of these steps into actions? Can it all be done in a single action and a single effect? You might be interested in this section of our performance article which recommends against sharing logic in a reducer via actions, and instead using plain functions. |
Beta Was this translation helpful? Give feedback.
-
Hi Brandon, Thanks so much for the very quick reply. I now understand that the 3 actions are sent one after another synchronously, but that the duration of the action is not taken into account. The reason I have separated these is because I created a general PrintClient:
I then create a general PrintFeature:
The reason for this is because this feature should be able to be used for various Apps that create documents. I'm a lawyer by trade and a hobby programmer. In my spare time I build form apps that generate (legal) letters in pdf. Having this function generalised allows me to use it for various apps. |
Beta Was this translation helpful? Give feedback.
Hi @tenthijeboonkkamp, the
await
inside theEffect.run
represents the async work to send the action on the main actor, not the lifecycle of the effect that is produced from sending the action.You can even confirm this by annotating the closure with
@MainActor
and seeing that you no longer have to await at all:This shows that the 3 actions are sent one after another synchronously, regardless of what logic and effects are being executed for each action.
Is there a reason you have separated all of these steps into actions? Can it all be done in a single action and a single e…