Data Control: Providing Information about Packages and their Installation
This tutorial demonstrates how you can share data between applications.
This feature is supported in mobile applications only.
Warm-up
Become familiar with the Data Control API basics by learning about:
-
Managing Data in Key-value Pairs
Get, add, update, and remove values assigned to a key.
-
Managing SQL-type Data
Select, insert, update, and remove SQL-type data.
Managing Data in Key-value Pairs
Learning how to manage map-type data allows you to use key-value pairs exposed by a DataControl provider:
-
Retrieve the MappedDataControlConsumer object using the getDataControlConsumer() method of the DataControlManager interface. This object allows accessing the key-value data stored by the DataControl provider.
You need a running DataControl provider application, which uses the "http://tizen.org/datacontrol/provider/DictionaryDataControlProvider" provider ID.
/* Get the map-type DataControlConsumerObject */ try { var globalMappedConsumer = tizen.datacontrol.getDataControlConsumer("http://tizen.org/datacontrol/provider/DictionaryDataControlProvider", "Dictionary", "MAP"); }
-
To meet the API requirement of using unique request identifiers, define a global variable, which is incremented every time a new request ID is needed. When the Data Control API responds to a request, it provides the request ID, which allows connecting the response with the specific request.
var globalReqId = new Date().getTime() % 2e9;
-
Define a common success and error callback:
function onRequestSuccess(id) { console.log("Request " + id + " - done"); } function onRequestError(id, error) { console.log("error in request " + id + ", message: " + error.message); }
- To manage key-value pairs:
-
To add a key-value pair, use the addValue() method of the MappedDataControlConsumer:
try { /* Increase globalReqId for uniqueness */ globalReqId++; globalMappedConsumer.addValue(globalReqId, "tizen", "Foo", onRequestSuccess, onRequestError); }
-
To retrieve values assigned to a key, use the getValue() method of the MappedDataControlConsumer:
function onGetValueSuccess(values, id) { console.log("Values retrieved in request " + id + ": " + values.toString()); } try { globalReqId++; globalMappedConsumer.getValue(globalReqId, "tizen", onGetValueSuccess, onRequestError); }
-
To update a value assigned to a key, use the updateValue() method of the MappedDataControlConsumer interface:
try { globalReqId++; globalMappedConsumer.updateValue(globalReqId, "tizen", "Foo", "Bar", onRequestSuccess, onRequestError); }
-
To remove a key-value pair, use the removeValue() method of the MappedDataControlConsumer interface:
try { globalReqId++; globalMappedConsumer.removeValue(globalReqId, "tizen", "Bar", onRequestSuccess, onRequestError); }
-
Managing SQL-type Data
Learning how to manage SQL-type data allows you to use databases exposed by a DataControl provider:
-
To retrieve a SQLDataControlConsumer object, use the getDataControlConsumer() method of the DataControlManager interface. This object allows accessing the data stored by the DataControl provider.
You need a running DataControl provider application, which uses the "http://tizen.org/datacontrol/provider/DictionaryDataControlProvider" provider ID.
/* Get SQL type DataControlConsumerObject */ try { var globalSQLConsumer = tizen.datacontrol.getDataControlConsumer("http://tizen.org/datacontrol/provider/DictionaryDataControlProvider", "Dictionary", "SQL"); }
-
To meet the API requirement of using unique request identifiers, define a global variable, which is incremented every time new request ID is needed. When the Data Control API responds to a request, it provides the request ID, which allows connecting the response with the specific request.
var globalReqId = new Date().getTime() % 2e9;
-
Define a common success and error callback:
function onRequestSuccess(id) { console.log("Request " + id + " - done"); } function onRequestError(id, error) { console.log("error in request " + id + ", message: " + error.message); }
- To manage the data:
-
To insert data, use the insert() method of the SQLDataControlConsumer interface:
function onInsertSuccess(reqId, rowId) { console.log("request: " + reqId + " succeed - inserted row id:" + rowId); } try { var rowData = { columns: ["WORD", "WORD_DESC"] , values: ["'tizen1'", "'tizen2'"] }; /* Increases globalReqId for uniqueness */ globalReqId++; globalSQLConsumer.insert(globalReqId, rowData, onInsertSuccess, onRequestError); }
-
To select data, use the select() method of the SQLDataControlConsumer interface:
function onSelectSuccess(result, id) { var length = result.length, i, j; for (i = 0; i < length; i++) { console.log("==== Row ", i, ":"); for (j = 0; j < result[i].columns.length; j++) { console.log("column: " + result[i].columns[j] + ", value: " + result[i].values[j]); } } } try { var columns = ["WORD", "WORD_DESC" ]; globalReqId++; globalSQLConsumer.select(globalReqId, columns, "WORD='tizen1'", onSelectSuccess, onRequestError); }
-
To update data, use the update() method of the SQLDataControlConsumer interface:
try { var rowData = { columns: ["WORD", "WORD_DESC"] , values: ["'tizen1'", "'Web apps platform!'"] }; globalReqId++; globalSQLConsumer.update(globalReqId, rowData, "WORD='tizen1'", onRequestSuccess, onRequestError); }
-
To remove data, use the remove() method of the SQLDataControlConsumer interface:
try { globalReqId++; globalSQLConsumer.remove(globalReqId, "WORD='tizen1'", onRequestSuccess, onRequestError); }
-