Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Optimizing API Requests for Samsung SmartTV Applications

The Ajax request is a memory-intensive JavaScript process. Simultaneous Ajax requests from a Samsung SmartTV application can cause device memory shortage that can result in a failed request or even halt the application.

Therefore, a developer needs to consider the preceding potential issue and be prepared for simultaneous multiple requests.

Minimize the number of API requests

In a general web bulletin board, selecting a list item causes the application to make a new request for detailed data of the selected item using its key code. However, allowing this habit in the SmartTV application development and making a new request each time a scene changes may cause memory shortage. It is better to make a small number of comprehensive requests. See the following code for the API.

<?xml version="1.0" encoding="UTF-8"?>
<API>
    <item>
        <itemId>001</itemId>
        <itemName>The 2011 SmartTV</itemName>
    </item>
    <item>
        <itemId>002</itemId>
        <itemName>The 2012 SmartTV</itemName>
    </item>
    <item>
        <itemId>003</itemId>
        <itemName>The 2013 SmartTV</itemName>
    </item>
    
</API>

The preceding API requests a list of items. Each item’s itemId key element is then used to make the next API request for an item’s detailed information.

<?xml version="1.0" encoding="UTF-8"?>
<API>
    <item>
        <itemId>001</itemId>
        <itemName>The 2011 SmartTV</itemName>
        <itemState>false</itemState>
        <itemDate>2013-01-31</itemState>
        <itemTitle>false</itemState>
        <itemContent>Hello World</itemState>
    </item>    
</API>

The preceding pattern is commonly used in standard web bulletin board development. Data requests completely rely on API design. And the preceding API design makes multiple API requests unavoidable. However, the next API design allows a single request to handle the preceding data access.

<?xml version="1.0" encoding="UTF-8"?>
<API>
    <item>
        <itemId>001</itemId>
        <itemName>The 2011 SmartTV</itemName>
        <itemState>false</itemState>
        <itemDate>2013-01-31</itemState>
        <itemTitle>false</itemState>
        <itemContent>Hello World</itemState>
    </item>
    <item>
        <itemId>002</itemId>
        <itemName>The 2012 SmartTV</itemName>
        <itemState>false</itemState>
        <itemDate>2013-01-31</itemState>
        <itemTitle>false</itemState>
        <itemContent>false</itemState>
    </item>
    <item>
        <itemId>003</itemId>
        <itemName>The 2013 SmartTV</itemName>
        <itemState>true</itemState>
        <itemDate>2013-01-31</itemState>
        <itemTitle>false</itemState>
        <itemContent>false</itemState>
    </item>
</API>

The first distributed API requests may help neat code management. But the preceding single request method is necessary for the application performance gain. The received response value can be stored within a DOM element, a Java Bean type object variable, or as an internal SmartTV file using the File API. The following code demonstrates storing response data in a DOM element.

jQuery.ajax(url, {
    type : 'GET',
    dataType : "json",
    success: function(res) {
        var result = jQuery(res).find('API > item');
        jQuery('#model').eq(1).attr('itemDate', result[1].find('> itemDate').text());
    }
});

Cache optimization

Another technique of optimizing the API requests is using cache, which stores a result in a variable using a unique ID, and reuses the stored value the next time the same data is needed, instead of making a new request. This is highly useful in an application with frequent API requests. The following code declares a variable to store the results.

var cache = {};    // global variable

Then the Ajax result is paired with a unique ID and stored as a member property of the cache variable.

jQuery.ajax(url, { 
    type : 'GET',
    dataType : "json",
    success: function(res) {
        var result = jQuery(res);
        cache['0011AA'] = result;    // unique id = 0011AA
    }
});

The result variable holding the API response is stored under the cache variable. If another request for the same data is received, then the code uses the cached data without making a new server API request.

if (!cache['0011AA']) {
    jQuery.ajax(url, { 
        type : 'GET',
        dataType : "json",
        success: function(res) {
            var result = jQuery(res);
            cache['0011AA'] = result;    // unique id = 0011AA
            callback(cache['0011AA']);
        }
    });
} else {
    callback(cache['0011AA']);
}

While the preceding code defines a static random ID, an ID management system needs to be implemented in a real application.

Cache optimization is a highly efficient technique that can eliminate many requests. However, it has its own limit on handling frequently updated data, since it reuses already stored data.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!