When working with the WRT Service API, you can often choose between a synchronous and an asynchronous version of the same method. This allows having the same functionality with a slightly different impact on the rest of your code.
This article describes how to handle synchronous and asynchronous calls and explains when the different call types should be used.
Contents |
Synchronous calls require only one argument, a criteria object that contains information relevant to the called Service API method. Since the call is synchronous, the JavaScript code execution will actually be blocked until the method returns.
A synchronous method call returns an object containing 3 properties:
Asynchronous calls require two arguments:
Being asynchronous, your code continues its natural execution without the need to wait for the called method to return. When the return information is available, the callback method will be called to handle it.
The object returned from an asynchronous call is different from the one returned from a synchronous one, and it will contain these properties:
The callback method is the method that is called when the asynchronous call has ended and the information it returns is available. It must be defined to accept three arguments:
Common callback implementations usually involve:
Not all methods support both synchronous and asynchronous calls.
For example, the IMessaging.GetList() method from the Messaging Service API supports only synchronous calls, while the IDataSource.GetList() method from Media Management Service API can only be asynchronous.
This sample use case shows how to retrieve the device contact list, with both synchronous and asynchronous calls. Error management is not handled in this example but you must handle it in your widgets.
First, instantiate the Service object:
var so = device.getServiceObject("Service.Contact", "IDataSource");
Now define the criteria object that will be used to retrieve contacts. This object is the same for both synchronous and asynchronous calls.
var criteria = new Object();
criteria.Type = "Contact";
Then, define a function that will parse the retrieved contacts. This can be equally defined to work with both calls:
function handleContacts(contacts)
{
try
{
contacts.reset();
var contact;
while((contact = contacts.getNext()) != undefined)
{
//handle current contact
}
}
catch(e)
{
alert('Error while showing contacts');
}
}
The synchronous version is quite straightforward. You only need to call the GetList() method and pass the returned information to the handleContacts() method:
var result = so.IDataSource.GetList(criteria);
handleContacts(result.ReturnValue);
Different from before, you have to define a callback method that will be called when the asynchronous GetList() call returns. In real applications, you also have to check both transIds, to check which method call is actually calling your callback method, and eventCode to check if the event is completed (the value must be 2).
function contactsCallback(transId, eventCode, result)
{
handleContacts(result.ReturnValue);
}
So, the previous (synchronous) method call will change, taking contactsCallback as the second argument. Also here, you should check the error returned by this call, and store the returned TransactionID property that will be used in the callback method.
var result = so.IDataSource.GetList(criteria, contactsCallback);
Bold text