appcelerator - execute function in the background?
-
26-09-2019 - |
Question
Nearly done my first titanium developer app however, the biggest issue (and lack of documentation of) is the ability to execute a function in the background.
I have a function that basically just queries an external json file and then loops through it inserting about 150 records in the database. This completely hangs the app until it's done.
I've tried a jquery async loop plugin and it works though the rest of the app is still very slow / responsive to any commands.
It has to be possible to execute a function in a background thread or something while the rest of the app is completely accessible.
Anyone know how to do this?
Solution
just put your code in a setTimeout:
setTimeout(function (){
/* your code */
},0);
The behavior is like a thread.
OTHER TIPS
I think its been a while since you asked this but I had the same issue today and I solved using the code from: Background function Acppcelerator
One solution here indicates the specific functionality will be available in a later release
http://developer.appcelerator.com/question/68231/background-thread--timer-thread--service-thread
Another solution is to create a event and fire it off since windows/views execute on a separate thread, Scroll down to the Events Section on this page
// fire the event for the task
Ti.App.fireEvent("doLongTask");
Ti.App.addEventListener("doLongTask", function () {
// doing long task...
});