[Tips and Tricks] How to get single value from a search result list


#1

Let’s say you make an api call to a backend to search for a row that meets some search criteria.
There are several cases that you know only a single row will meet that criteria. .e.g you were searching for a row with some unique value.

Problem:
Even though you expect your result to return a single row, your endpoint may be returning a list of rows.
For example all calls made to bubble endpoints /obj/thingname returns a list of thingname.
In bubble you will normally call the :first or :last function on a search result to get the first or last item. If you expect the result to have only a single item asking for the first or last item will return just that single item for you.

Unfortunately there is currently no feature like this in dropsource for us to get a specific element (first, last, or random) from a search result that is a list.

Tip and Trick:
So here is my workaround for this problem.
The idea is basically to use the Iterate through Array action in the 200 response (or whatever your success response is) on the search result list.
First create a variable that is of the type that your search result returns.

  1. Add an Iterate through Array action
  2. Set the list property of the action to the event data then response result.
  3. Then inside the loop (item) event set the variable defined is step 1 to the current item.

So essentially you’re telling dropsource to overwrite the value of the variable with the current item in each run of the iteration (loop).
But since your list has only one item, you’re simply going to run the loop only once.

What if my list returns more than one item but i want the first or last?
An extension of this tip is the case where your search result returns more than one item but you want to get say the first or last item.
In this case you will do the same thing as above only difference is that you will need to make sure your search result is sorted in a way that the item you’re interested in is the last item in the search result.
If you’re using Bubble backedn, you can define the sort order as well as the field to sort by using from the API Parameters tab in Dropsource by selecting the Query field.

And since you’re overwriting the value in every step of the iteration, at the end of the iteration value in the variable will be the last value.

And there is no performance hit even if your search result contains 100s of rows and you’re overwriting the variable in each step.
You can even avoid the repetitive overwriting of the value by doing it only once.
You could define a boolean variable called say first_run and before the Iterate through Array action set this value to false.
Then in the item event of the iterate action, add an if-else action that checks if first_run is false then set your variable to the item value and also change the first_run to false.
This means that you will only write to your variable only once and in the first step because of the if-else condition.

I use these tricks and lot in my apps and they really work well.

Hopefully Dropsource will give us actions to get the first, last, or random item from a list of results soon. In the meantime lets keep hacking.:smiley:


Feature Request: Dynamic list tiles with % constraints, not just pixels
#2

I never thought of doing this! However I do agree it would be good to have some of this stuff available out of the box… :slight_smile:


#3

Have you tried this in the IOS app? Have you tried to use table view to display an array in the IOS? Does that work?


#4

I am sorry. I posted it incorrectly.
I was wondering if this tips and tricks work for IOS? Were you able to bind any array data from the API to the table view in the IOS?


#5

Hi @ateker01, i did my test in android so i can’t confirm if it will work on IOS too.
But technically i don’t see why it shouldn’t.

Also note that this workaround was for getting a single value out of a search which normally returns a list.
What you’re asking (i.e. binding an array data to a table view) is a totally different thing.
In response to that, yes i’ve successfully bound an array data from an api to a table view in IOS.


#6

This is great! Works like a charm and solves formatting issues that you can have across different devices (i.e. small vs. big screens) when using dynamic list tiles because the sizing cannot be done in percentages, only pixels. This can make a pretty big difference for how users perceive the design of your app in my view. I will be using this a lot.

Pretty brilliant trick, kudos to you Sean.
tenor


#7

Hi @seanhoots

Regarding the second half of the instructions about using the ‘descending’ and the ‘sort_field’ in the parameters section. - What would you send in those parameters? For example for the ‘descending’, would it just be a ‘1’?

I only want one result and I want the most recently created result by a user.


#8

The “sort_field” is the name of the field you want to sort by.
For example if you table has the fields, email, name, id, age and you want your results to be sorted by the age, then your sort_field will be age.
By default bubble sorts in descending order, that is, from large to small values. If you want this order you can leave the ‘descending’ parameter blank.
But if you want to sort in ascending order, that is from small to large, then you will have to make the descending parameter False. It is a boolean field so the values you can provide are True or False (note as booleans not text)


#9

Thanks a lot for the clarification. Works really well.