Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
help with asyncCall or coroutines to show a loader/spinner when app is hanging — Gideros Forum

help with asyncCall or coroutines to show a loader/spinner when app is hanging

piepie Member
edited April 2023 in General questions
Hi, I think I need some help on how to use coroutines or asyncCall.

I have an app using imgui that might be hanging here and there on older devices because it needs to load a bunch of entries from an sqlite db.
My goal would be to display something that moves/rotates/changes while those values load, but I am struggling already to understand how to manage this in the least invasive way (the app is working, I'd like to avoid rewriting a lot of stuff).

This pseudocode represent what happens now:
 
function sqliteload(query)
	local stmt = {}
	local output = {}
	local db = sqlite3.open("|D|"..dbfilename)
	stmt = db:prepare(query)
 
	for data in stmt:nrows() do
		output[#output+1] = data
	end
 
	return output 
end
 
 
local List = {}
function drawWindow()
--imgui stuff to draw window and display the content of List
 
		if ui:button("load list") then
			List = sqliteload("SELECT * FROM stuff") 	--this is where the app might hang, therefore I'd like to call the loader just before this line and show it until I have a complete list from sqliteload().
		end
 
end
 
function enterframe()
	ui:newFrame(e.deltaTime)
	drawWindow(ui)
	ui:render()
	ui:endFrame()
end
How/where should I add my loader?
I thought I could use this approach https://forum.giderosmobile.com/discussion/comment/56498/#Comment_56498
Core.asyncCall(sqliteload) but I get an invalid argument (function expected, got table), and it makes sense since a table is what I am returning there.. so how do I do it? :)

Thank you

Comments

  • hgy29hgy29 Maintainer
    edited April 2023 Accepted Answer
    pie said:


    Core.asyncCall(sqliteload) but I get an invalid argument (function expected, got table), and it makes sense since a table is what I am returning there.. so how do I do it? :)

    I think yout actually wrote
    Core.asyncCall(sqliteload("request"))
    while it should have been called like
    Core.asyncCall(sqliteload,"request")
    .
    But you'll lose the result of your call. Your function should either set the result as a variable that will be picked up by your enter frame call once populated, or call a callback function with the result, etc.

    EDIT: You can also do
    Core.asyncCall(function() List = sqliteload("SELECT * FROM stuff") end)

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.