Forum Documentation Showcase Pricing Learn more

How can I force an action to finish before the next one starts?


#1

Hi team,

I have several issues with my app, that are all related to the same problem: Bubble seems to start an action before the previous action is finished. but I need the previous action to be finished, before the next starts:

in my app, users can choose a medical issue, based on which the app creates a list of suitable rehabilitation exercises for the user. The list is stored in the field User’s exercises (see screenshots).



Every day the user receives a shorter list of rehabilitation exercises that he should do on that day. This list is stored in the field Today’s exercises (see screenshot below ( also see screenshot for Data Type User and Exercise above)).

The list Today’s Exercises is created as described below. As indicated I have problems with most steps:

1. The list of the previous day is deleted - Problem: the action delete is not finished before the API workflow starts. This creates inconsistencies in the database.

**2. An API workflow is called that produces the new list. It takes the parameter User’s Exercises sorted by Number of times performed. (See screenshot below) ** - Problem: The API workflow starts, before the list is fully sorted. This way the API workflow takes the wrong exercises.

51

3. The API workflow takes a line fo the table User’s exercise and creates a new line of the table Today’s exercise.

The workflow has to fill 2 fields in each line of Today’s exercise: It needs to copy the exercise and calculate the variable total workout length in seconds (see screenshot)

58

Problem: The calculation of the variable total workout length in seconds requires the previous line of the table Today’s exercise, but sometimes it’s not yet there, because Bubble starts a new loop on the API workflow, before the previous line is finished. This causes to many lines and inconsistencies in my list Today’s exercise.

Note: the variable total workout length in seconds defines how long the final Todays Exercise list will be. The API workflow will stop once the desired total workout length in seconds is reached (e.g. 300 seconds).

Thank you so much for the help! Please let me know if I need to clarify something.

Nicola


#2

I’ve not read your whole message, but am responding to your overall question about synchronous vs asynchronous behavior in Bubble.

Like all JavaScript and like all node.js apps (which, at the end of the day, is what your Bubble app IS), Bubble is essentially asynchronous – meaning that steps in a workflow are NOT executed in sequence UNLESS it is OBVIOUS that one later step DEPENDS UPON the results of a previous step. (Sorry for the caps, they are important, right?)

Bubble MOSTLY intelligently understands when one step depends upon another. But sometimes this breaks down and we have to help it. In a client-side (in page) workflow, we have a tool to make this obvious – the pause step. In a server-side workflow, we have to be a bit more careful. (There is no such thing as a server-side pause.)

The tools then are (1) pause and (2) “Only when” … evaluate the result of some previous step. Both of these will force synchronous behavior.

This is not a bug, BTW. We desire Bubble to be as fast as possible.


#3

Why does “pause” enforce synchronicity? Because the step AFTER A PAUSE can only be executed once the pause is done. And the pause CAN ONLY START after the previous step has executed. So this enforces a clean break between the step BEFORE the pause and step AFTER the pause, eh?

Why does “results of step x” enforce synchronicity? Because the RESULT of a given step CAN ONLY be evaluated once the given step has completed execution.

I hope that these tips help you!


#4

Hi Keith,

thank you for your answer. The solution to add a pause seems to help with my first issue and it might also solve the second (I am not 100% sure yet), but I still have the issue that the API workflow loops into the next line, before the previous line is fully processed.
That is an issue, because I do a calculation that needs input from the previous line.

thanks for helping!

Nicola


#5

Pause is client side only, and have technically no duration effect on server side. If your client by accident refresh the screen during the saving process (and you are using states for saving values), the client will loose probably some info, as states are saved by page.

For the API workflow, I recommend to start your API, check for a status: completed? yes, then execute the process. If still completed?=no, rescheduled the api to himself in 5 seconds (or whatever amount of time you need). The loop will terminated as completed?=yes, eventually.


#6

Make your second calculation dependent on a new object, created in a previous step. At the end of the api workflow, you can delete this temporary object.

Example: we need to calculate an integer, the sum of two things. Thing 1 is already in the database. Thing 2 is also in the db as well.

Step one make a temp thing: create a new thing: what thing? Temp object. Field to change? An_integer. Value of An_integer? Do a search for Thing 1’s integer field.

Step 2 do the calculation in the temp thing: make changes to a thing. What thing? Results of step 1. Field to change? An_integer. Value of An_integer? Do a search for Thing 2’s integer field + This thing’s An_integer. (I.e., n = n+x)

Step 3 save our calculated value to some other field in some other object: Make changes to a thing. What thing? Whatever your target is. Field to change? Some integer field on this thing. Value? Results of step 2’s An_integer.

Step 4 delete the temp thing: delete a thing. What thing? Results of step 1. Only when? Result of step 3’s some integer field is not empty.

… or something like that. In this way you can force synchronous behavior, by forcing each action to depend on the results of the previous action.


#7

Hi Keith,

this solution will help me to enforce that one step within a workflow is done before the next starts, right?

I might not fully get it, but I think my issue is different: I do an API workflow on a list and I need Bubble to finish processing a row of this list, before it starts processing the next row.

E.g.: I run an API workflow on a table with 3 rows:

When the API workflow accesses the first row I calculate an Integer “total workout time” = 60 and add it to a new list that I am creating.

When the API workflow accesses the second row I calculate the Integer “total workout time” = “total workout time” from the first row + 60 = 120.

When the API workflow accesses the third row I calculate the Integer “total workout time” = “total workout time” from the second row + 60 = 180.

The above explanation is a simplification. What is important: The previous row has to be processed, otherwise the next row cannot be processed.

For better understanding, the screenshots below show how I programmed the API endpoint:


Thanks for getting into this Keith!

Nicola


#8

Hmm… Not sure if this will help you, but note that if all you are doing is summing some numbers from a list, you don’t have to iterate over them manually (as in a schedule API Workflow on a list).

Let’s say you have a list of workouts, each of which has a duration (that is a number representing minutes, for example). The list of durations is:

list_of_workout's durations

Go ahead and print that out. You’ll see that expression is just a list of numbers.

The sum of those durations is just:

list_of_workout's durations:sum

? helpful ?


#9

Hi Keith,

thanks, but that doesn’t solve my issue. The API workflow creates a new list with exercises called Today’s Exercises. The calculation of the total workout length just helps me determine when to stop adding new lines to the list.

As far as I understand I need the API workflow to create the list.

Any other ideas?

Nicola


#10

You could try using a recursive workflow instead of scheduling an API workflow on your list.