I'm building an app and at one point in the app I need to construct a tableView that contains 3787 items in it. (There's a search bar at the top so the user doesn't have to scroll all the way down). However it takes a good 5 seconds to insert the array into the tableview, leading to loading time when the app starts up or before going to that scene. Is there a way to trim this time down? I thought of multithreading and looked up Lua coroutines but don't completely understand the implementation to get them running asynchronously. Or how to have a loading bar while the table is loading. The table is in another scene so im using stoyboard.loadScene()
I see three options:
I get the impression that you can handle 1 and 2, and most likely #4 too. For 1 and 2 you need to provide some indication that a load operation is taking some time, but otherwise nothing too difficult. For 4 there is no progress needed but you need to determine which rows to load based on the "view" of table (subset of rows visible).
Option is technically more challenging. You are right that you should use coroutines. They are actually quite easy to use:
thread = coroutine.create(loadTable)
loadTable should be designed to do only small chunks of work at a time, and yield in between chunks, such as
function loadTable()
...init...
coroutine.yield()
while haveMoreRows do
read 10 rows
coroutine.yield()
end
...cleanup...
end
your code resumes the thread repeatedly, until the thread dies: coroutine.resume(thread)
. A good place to do this would be in the enterFrame handler of corona's Runtime since this is called at every time frame.
function enterFrame(e)
if thread ~= nil then
if coroutines.status(thread) == 'dead' then
create table display so it is instantly available in table scene
if showing progress, hide it
thread = nil
else
coroutine.resume(thread)
end
end
In your scene transition (to the scene that shows the table), you should check if thread is not nil, if so then the load is not yet done so you show the message (in new scene) that table is loading; the message will get removed in the enterFrame as soon as load completed.
An important thing to know about a coroutine (cooperative thread) is that the threaded function can have multiple yield points; at the next resume, the function continues to execute from where it left off, with the correct local state.
Hopefully you have looked at http://lua-users.org/wiki/CoroutinesTutorial.