locked
Windows 8.1 Phonegap, WebSQL plugin, Call stack and seemingly synchronous javascript

    Question

  • Hi,

    the app I developed with Phonegap is working fine on Android and iOS, but it uses a database so the development for Windows was delayed. Because Windows did not support openDatabase calls via Phonegap apps.
    This all changed when the MS Open Tech WebSQL plugin was released.

    Call stack

    After a short while it became clear that the call stack of a Windows 8.1 device is limited compared to the other platforms.
    After running a few tests  I determined that my Call Stack was limited to about 330, which is not very much when working with a big and complicated data set. This forced me to rethink my itterate logic of the data, bringing me to my next problem. The example below contains the logic which does not work on windows 8.1 due to call stack limitation. The large call stack is caused by a function calling itself on a database-succes-call, this method ensures javascript to act as synchronous as possible.

    example:

    tx.executeSql("SELECT * FROM PARENT", [], function(tx, results){
    var total = results.rows.length;
    var counter = 0;

    insert(tx);

    function insert(tx){
    if(counter < total){
    tx.executeSql("INSERT...", [], function(txSucces, results){
    var insertId = results.insertId;
    var otherData = results.rows.item(counter);
    getSubRecords(tx, insertId, otherData);
    });
    }
    }

    function getSubRecords(tx, insertId, otherData){
    tx.executeSql("SELECT * FROM CHILD WHERE otherData = ?", [otherData], function(tx, results){
    tx.executeSql("INSERT...", [insertId, ...], function(tx){
    counter++;
    insert(tx);
    });
    });
    }
    ....


    Seemingly synchronous javascript.

    Because the app needs insert ids of new records it's not just an easy insert method.
    At some level a number of records are inserted in a for loop and in every succes call important data + insert-id of every record is inserted in an array. When the for loop enters it's final loop al sub-records per previous inserted record are collected from the database and then inserted, also with a for loop. Normally you wouldn't expect such logic to work in javascript, being a-synchronous. But it worked as if the WinJS engine works synchronous. 

    This doesn't work with Android nor iOS, so it means I have to maintain to different sets of code. 

    example:

    tx.executeSql("SELECT * FROM PARENT", [], function(tx, results){
    var total = results.rows.length;
    var counter = 0;
    var arr = new Array();

    for(counter; counter < total; counter++){

    tx.executeSql("INSERT...", [], function(txSucces, results){
    arr.push({"insertId":results.insertId, "otherData":results.rows.item(counter)});
    if(counter == (total-1)){
    for(var counter2=0; counter2 < arr.length; counter2++){

    tx.executeSql("SELECT* FROM CHILD WHERE otherData = ?", [arr[counter2].otherData], function(tx, results){
    var childTotal = results.rows.length;
    for(var counter3 = 0; counter3<childTotal; counter3++){

    tx.executeSql("INSERT...", [arr[counter2].insertId, ...]);
    }
    });
    }
    }
    });
    }
    ....

    Does anyone know what causes this behaviour or knows how to fix one of these two problems:
    - low maximum call stack
    - synchronous javascript behaviour

    Thank.

    Tuesday, November 4, 2014 9:53 AM

Answers

  • Hi P.Ven,

    I'm not familiar with WebSQL, probably I cannot give any suggestion on that.

    But I don't know if you have read the documentation: Asynchronous programming in JavaScript, call stack looks like managed by system, we cannot do anything with it, however promise seems can help with such situation, this also the suggestion for your second problem.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, November 5, 2014 5:48 AM
    Moderator