I've got some strange loop in my app when I trigger getTotalRows() in the success event of getRows(). You would think that I somehow have getTotalRows() trigger getRows() again. But this isn't the case. I can only stop it by removing the trigger in the success event section.
My setup:
getRows() is set to automatic
getTotalRows() is set to manual
The controlled by section of getTotalRows() shows that it is controlled by getRows() but that it doesn't control getRows():
Thanks for reaching out! I'm not sure yet why this is happening Sometimes the debugger notes aren't quite as granular as we'd like
If you're willing to share a json export of the app I can take a closer look! Otherwise, I'd be curious to see screenshots of the queries. Is getTotalRows returning a count of all possible records?
Is it an option to trigger getTotalRows independently (i.e. once on page load, and then on success of any write queries)?
I don't think that's possible. Due to the fact that the query is triggered again when filters change. It's not a small query. It works fine though. Especially when it's cached.
The reason I need it to be set to automatic is because the pagination stops working in retool mobile. I've got a topic about that as well. I'll add it below in case people want to read about it.
getTotalRows is the total amount of records taking in consideration the filters that are set.
getRows():
SELECT
a.id AS lineitemsId,
*,
CAST((
SELECT COUNT(*)
FROM app_variants v2
WHERE v2."productId" = a."productId"
AND v2.variant_stock = true
) AS INT ) AS inStockVariantCount
FROM app_lineitems a
JOIN app_products p ON a."productId" = p.id
JOIN app_variants v ON a."variantId" = v."variantId"
JOIN app_lineitem_properties lp ON a.id = lp."lineitemId"
WHERE
((lp.picked = {{ getTabFilter.value }} AND lp.source = 'order')
OR (lp.source <> 'order' AND lp.restocked={{ getTabFilter.value }} AND lp.restock_location = 'order'))
AND lp.archived = {{ archivedSwitch.value }}
AND p.ignore = {{ ignoreSwitch.value }}
AND (
({{ preorderSwitch.value }}::boolean IS NULL OR {{ preorderSwitch.value }}::boolean = true)
OR lp.preorder = false
)
AND (
({{ orderedSwitch.value }}::boolean IS NULL OR {{ orderedSwitch.value }}::boolean = true)
OR lp.ordered = false
)
AND (
({{ lostSwitch.value }}::boolean IS NULL OR {{ lostSwitch.value }}::boolean = true)
OR lp.lost = false
)
AND (
({{ inTransitSwitch.value }}::boolean IS NULL OR {{ inTransitSwitch.value }}::boolean = true)
OR lp.in_transit = false
)
ORDER BY a.created_time DESC
LIMIT {{ lineItemList.pagination.pageSize }}
OFFSET {{ lineItemList.selectedPageIndex * lineItemList.pagination.pageSize }};
getTotalRows()
SELECT COUNT(*)::int AS count
FROM app_lineitems a
JOIN app_products p ON a."productId" = p.id
JOIN app_variants v ON a."variantId" = v."variantId"
JOIN app_lineitem_properties lp ON a.id = lp."lineitemId"
WHERE
((lp.picked = {{ getTabFilter.value }} AND lp.source = 'order')
OR (lp.source <> 'order' AND lp.restocked={{ getTabFilter.value }} AND lp.restock_location = 'order'))
AND lp.archived = {{ archivedSwitch.value }}
AND p.ignore = {{ ignoreSwitch.value }}
AND (
({{ preorderSwitch.value }}::boolean IS NULL OR {{ preorderSwitch.value }}::boolean = true)
OR lp.preorder = false
)
AND (
({{ orderedSwitch.value }}::boolean IS NULL OR {{ orderedSwitch.value }}::boolean = true)
OR lp.ordered = false
)
AND (
({{ lostSwitch.value }}::boolean IS NULL OR {{ lostSwitch.value }}::boolean = true)
OR lp.lost = false
)
AND (
({{ inTransitSwitch.value }}::boolean IS NULL OR {{ inTransitSwitch.value }}::boolean = true)
OR lp.in_transit = false
)
It's basically the same query but without the limit and offset and COUNT added.
Topic about pagination not working when resource query is set to manual:
I will look into it further, so apologies if this is short sighted, but in my initial testing, you can have getRows and getTotalRows both as separate, automatic queries (no event handlers on either query).
getRows and getTotalRows will both run any time the filters change, but only getRows will trigger when the user clicks through the different pages
I've been fixated on this part that getRows should invoke getTotalRows all the time. But if they are both automatic, they both trigger on the filter changes. I'll just have to remember to trigger them both in case they are not triggered by an input that changed.
That fixes the infinite loop. But I still can't wrap my head around why there's an infinite loop when getRows triggers getTotalRows
I'm happy I got this working though. Thanks a lot!
(so not short sighted at all. It seems it has fixed my problem )
I think when getTotalRows runs, it causes the list to re-evaluate, (even if the total isn't changing), which then causes getRows to run. But I'm not seeing the same issue on the web app side with paginated tables, so I'll do some more research in case there's a bug
I haven’t tested it on web / mobile. Since it goes beserk right away in the editor .
I would get it that that’s the reason why it becomes a loop. But it shouldn’t right? You would expect that it will only re-run if the variables change. Because we don’t have control over components that reevaluate.