Solving the ‘QUOTA_EXCEEDED_ERR’ message when using local storage on Motorola RhoElements browsers

Working with JavaScript exceptions can be quirky as descriptions of these exceptions sometimes does not make any sense. In this particular case I was working on a Motorola device lately which was running the RhoElements 4.x web browser. The Motorola RhoElements browser is part of the RhoMobile Suite of Motorola, which allows web developers to have a closer hardware integration with Motorola devices.

A strange bug popped up however while I was trying to use local storage to store offline data. The QUOTA_EXCEEDED_ERR exception pops up when I try to store local data. This error is known tot pop up when you use Mobile Safari in Private Browsing Mode but I’m not using Mobile Safari obviously. This is the code snippet I used to set data into local storage:

set: function (name, value) {
    try {
        localStorage.setItem(name, value);
    } catch (e) {
        if (e == QUOTA_EXCEEDED_ERR) {
            alert('Quota exceeded!');
        }
    }
}

Browsing the Motorola forums finally led to a discussion mentioning both this erratic behavior as well as the solution. I was in fact using a dash (‘-‘) in my host name which leads to this strange behavior. It has been promised by a Motorola service desk employee to be solved in 4.2. Currently I’m working on 4.0 so unfortunately I can’t check this. Nevertheless, I’m using the IP address instead of the FQHN for now to bypass this issue.

AX Deleting items from a map

I faced a challenge in deleting specific items from a map today. The problem occurs when the recordIterator.delete() x++ statement is executed. Any consecutive statement using the recordIterator variable will throw the exception ‘the iterator does not designate a valid element’. According to SysDictCoder it is not a problem to remove items using the ListIterator though.

The solutions lays within the use of a set in which the keys of the items that needs to be deleted are stored. After the first iteration another iteration takes place to actually delete the applicable items from the map.

public Map cleanupRecordMap(Map _recordMap)
{
    MapIterator     recordIterator = new MapIterator(_recordMap);
    Set             deleteRecordSet = new Set(Types::String);
    SetEnumerator   deleteRecordSetEnumerator;
    ;

    if(_recordMap.elements() > 0)
    {
        while(recordIterator.more())
        {
            if(true)
            {
                // mark record to be deleted by adding it to a set
                deleteRecordSet.add(recordIterator.domainValue());
            }
            
            recordIterator.next();                                 
        }
                
        // use the set of keys to remove them from the map
        deleteRecordSetEnumerator = deleteRecordSet.getEnumerator();
        while(deleteRecordSetEnumerator.moveNext())
        {
            _recordMap.remove(deleteRecordSetEnumerator.current());   
        }
    }       

    return _recordMap;
}

Export enumerations of an AX project using AOT traversal

While working on the documentation phase for an AX 2012 project I needed a way to speed up a few things. One of the requirements for the documentation was to add a description of all enumerations that has been used by this project. The result was a simple but powerful script which recursively traverse all nodes of a project to find all enumerations. For each enumeration that is found the actual values are printed.

Of course it is a very specific script for a certain purpose but you can of course change it to gather other kinds of information.

static void JM_ExportProjectEnumValues(Args _args)
{
    ProjectListNode sharedProjList = infolog.projectRootNode().AOTfindChild("Shared");
    ProjName projName;
    ProjectNode projNode;
    ProjectNode projNodeLoad;

    #TreeNodeSysNodeType

    // warning: recursive function
    void traverseProjectNodes(ProjectNode _rootNode)
    {
        TreeNode childNode;
        TreeNodeIterator rootNodeIterator;
        TreeNodeType treeNodeType;
        DictEnum dictEnum;
        int enumValueIdx;
        ;

        if (_rootNode)
        {
            rootNodeIterator = _rootNode.AOTiterator();
            childNode = rootNodeIterator.next();
            while (childNode)
            {
                treeNodeType = childNode.treeNodeType();
                //info(strfmt("%1: %2 - %3", treeNodeType.id(), rootNode.AOTname(), childNode.AOTname()));

                // found group: jump in
                if (treeNodeType.id() == #NT_PROJECT_GROUP)
                    traverseProjectNodes(childNode);

                // found base enum
                if (treeNodeType.id() == #NT_DBENUMTYPE)
                {
                    dictEnum = new DictEnum(enumName2Id(childNode.AOTname()));
                    if(dictEnum)
                    {
                        info(strFmt("ID:%1 NAME:%2", dictEnum.id(), dictEnum.name()));
                        for(enumValueIdx = 0; enumValueIdx < dictEnum.values(); enumValueIdx++)
                        {
                            info(strFmt("%1 = %2", dictEnum.index2Value(enumValueIdx), dictEnum.index2Label(enumValueIdx)));
                        }
                    }
                }
                childNode = rootNodeIterator.next();
            }
        }
    };

    // traverse all enums of a given project
    projName = ""';
    projNode = sharedProjList.AOTfindChild(projName);
    projNodeLoad = projNode.loadForInspection();
    traverseProjectNodes(projNodeLoad);
    projNodeLoad.treeNodeRelease();
}

Use an offline storage engine to store data in the browser

Use an offline storage engine to store data in the browser

At the moment of writing this article 2 off-line storage engines are available to the majority of the modern browsers. These database engines are also known as Web SQL Database and Indexed Database. While both serve more and less the same purpose they are totally different in use and in their capabilities.

Web SQL is basically a SQLite implementation and thus considered to be a relational database engine. In other words, you can use SQL to query data from tables and relations between tables exist to allow you to store and query data relationally. Indexed Database on the other hand stores records based on keys and indexes similar in having key-value lists. No query language is available so you have to write your own mechanism to get access to your data. Especially when it comes down to handle more complex data structures it can become a challenge.

Unfortunately, according to the W3C specification Web SQL is officially set to a standstill due to a lack of competition. The only  ‘future proof’ alternative to store a considerate amount of off-line data is the Indexed Database which is specified here. Also, according to the browser support table on HTML5Rocks.com both engines are not fully supported by all major browsers so that gives most of the web developers a big challenge: Do I need to write a Data Access Layer to support Web SQL, Indexed Database or both?

In the upcoming weeks I will write and post more articles to guide you through the wonder-world of both Web SQL and Indexed Database so you will have all the code and background information you need to build your own off-line storage engine. If you have any questions or remarks then please let me know.