Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked May 11th 2010 by Damien Elmes
  • database
  • threading
0 Comments

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..

    — answered May 13th 2010 by Damien Elmes
    permalink
    0 Comments
  • 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).

    — answered May 12th 2010 by Damien Elmes
    permalink
    0 Comments
  • 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 :)

    — answered May 13th 2010 by David Ashwood
    permalink
    0 Comments
  • 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.

    — answered May 13th 2010 by David Ashwood
    permalink
    0 Comments
  • Perhaps the problem was testing in a single window. I'll look into creating another window and see if that helps.

    — answered May 13th 2010 by Damien Elmes
    permalink
    1 Comment
    • How did this work for you? Did the multiple windows help with this problem?

      — commented July 14th 2011 by Mitch Schwenk
  • 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.

    Link to the plugin

    Greets,
    Dennis

    — answered September 27th 2010 by Dennis Schneider
    permalink
    0 Comments
  • 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.

    — answered May 11th 2010 by David Ashwood
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.