Asynchronous DB, threads or worker processes in Titanium mobile?
Is there any way to do processing in the background? At the moment I'm updating my database just after displaying information to the user, but if they move too quickly they have to wait for the DB to catch up.
7 Answers
-
I appreciate you trying to help.
The reason I made the while loop infinitely long is to demonstrate that other events are not being serviced until the loop ends. So if I am doing an expensive DB update in the background, the user will click on a button and nothing will happen. I don't see how generating a separate stack & custom event buys me anything over my current approach of doing the heavy processing after an interface update..
-
Sadly, it doesn't seem to work:
var win = Ti.UI.createWindow({title:"mywin"}); win.open(); var b = Ti.UI.createButton({title:"foo"}); win.add(b); b.addEventListener("click", function () { Ti.API.debug("clicked"); Ti.App.fireEvent("foo"); Ti.API.debug("clicked2"); }); Ti.App.addEventListener("foo", function () { while (true) { } });
After the first button press, the click signal is not fired again (presumably until the infinite loop terminates).
-
Try something more like:
var dataStack = new Array(); Ti.App.addEventListener("foo", function() { // You can pass the data as an arg or use the dataStack for(var i=0;i<dataStack.length;i++) { //do something with dataStack[i] } }); var win = Ti.UI.createWindow({title:"mywin"}); win.addEventListener("click", function(e) { dataStack.push({foo:"bar"}); Ti.App.fireEvent("foo"); }); var b = Ti.UI.createButton({title:"foo"}); win.add(b); win.open();
The while loop never ends - so you're chewing up processing cycles without needing to.
Just push the data & process the data stack.Note: The above code will probably need tweaking to run properly :)
-
It gives you a disconnect between generating the data & processing it.
It's helps in a number of circumstances - including animation where you generate an update to the data for the animation (following the finger position which fires many touchmove events) and the updating of the image.It allows you to exploit some of the properties of Titanium where each window runs with it's own thread - essentially allowing the message pump breathing space.
Specifically in the above example - you might quickly push 5 items onto the dataStack - which is then processed in one batch by the event receiver. It's a common programming solution to this type of problem.
-
Perhaps the problem was testing in a single window. I'll look into creating another window and see if that helps.
-
Hi there, I had the same problem. When making too many calls to the database, the user interface gets stuck. Fortunately I am using jQuery and I found a pretty cool plugin named jQuery Async. With the plugin it is possible to make JavaScript calls without interrupting the user interaction.
Greets,
Dennis -
I think events are async. A good general approach is to push the data onto a stack and fireEvent for a synthetic event. Then in the event just process the stack.
Also checkout the events section here - https://developer.appcelerator.com/apidoc/mobile/1.2/Titanium.UI.Window-object.html
It explains how windows are on a separate thread from the calling context - so the eventing approach above should work.