Skip to main content

Saving, acquiring, and deleting application data

Saving application data​

OpenSocial allows data to be saved in a container. Application data in a container can be saved, acquired, or deleted through the API.

The following contains sample code for saving an application:

var req = opensocial.newDataRequest();
req.add(req.newUpdatePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, "score", 1000), "res1");
req.add(req.newUpdatePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, "rank", "A"), "res2");
req.send(function(response){
if (response.hadError()) {
// error
} else {
// ...
}
});
  • Call opensocial.newDataRequest() and create a DataRequest.
  • Call newUpdatePersonAppDataRequest() in the generated DataRequest and generate a request.
  • Specify the owner of the application data (VIEWER cannot be changed), the data key and values in the arguments.
  • Call DataRequest.add(request) and register the request.
  • Call DataRequest.send(callback) and make a request to acquire information.
  • Once the server request process is complete, the callback function specified with send() is executed using the DataResponse that contains the request results as an argument.
  • Process the data that is stored in DataResponse.

Acquiring application data​

The following contains sample code for acquiring application data that has been saved by a VIEWER:

var params = {};
params[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
var idSpec = opensocial.newIdSpec(params);
var req = opensocial.newDataRequest();
req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
req.add(req.newFetchPersonAppDataRequest(idSpec, ["score", "rank"]), "result");
req.send(function(response) {
if (response.hadError()) {
// error
} else {
var viewer = response.get("viewer").getData();
var result = response.get("result").getData();
var score = result[viewer.getId()]["score"];
var rank = result[viewer.getId()]["rank"];
// ...
}
});
  • Generate the IdSpec that indicates the owner of application data using opensocial.newIdSpec(). Now specify the VIEWER.
  • Call opensocial.newDataRequest() and create a DataRequest.
  • Call newUpdatePersonRequest() from the DataRequest that was generated to acquire information on the VIEWER, and generate a request.
  • Call DataRequest.add(request) and register the request.
  • Call newUpdatePersonAppDataRequest() and generate a request.
  • Specify the IdSpec, which identifies the owner of the application data, and the data key with an array in the arguments.
  • Call DataRequest.add(request) and register the request.
  • Call DataRequest.send(callback) and make a request to acquire information.
  • Once the server request process is complete, the callback function specified with send() is executed using the DataResponse that contains the request results as an argument.
  • Process the data that is stored in DataResponse.

Next, acquire the application data that was saved by the VIEWER's Mobatomo (Friend).

var params = {};
params[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
params[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.FRIENDS;
var idSpec = opensocial.newIdSpec(params);
var req = opensocial.newDataRequest();
req.add(req.newFetchPersonAppDataRequest(idSpec, ["score", "rank"]), "result");
req.send(function(response) {
if (response.hadError()) {
// error
} else {
var result = response.get("result").getData();
for (var id in result) {
var score = result[id]["score"];
var rank = result[id]["rank"];
// ...
}
}
});
  • Generate the IdSpec that indicates the owner of the application data using opensocial.newIdSpec(). Now specify the VIEWER’s FRIENDS.
  • Call opensocial.newDataRequest() and create a DataRequest.
  • Call newUpdatePersonAppDataRequest() and generate a request.
  • Specify the IdSpec, which identifies the owner of the application data, and the data key with an array in the arguments.
  • Call DataRequest.add(request) and register the request.
  • Call DataRequest.send(callback) and make a request to acquire information.
  • Once the server request process is complete, the callback function specified with send() is executed using the DataResponse that contains the request results as an argument.
  • Process the data that is stored in DataResponse.

Deleting application data​

Follow the steps below to delete saved application data:

var req = opensocial.newDataRequest();
req.add(req.newRemovePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, ["score", "rank"]);
req.send(function(response) {
if (response.haError()) {
// error
} else {
// ...
}
});
  • Call opensocial.newDataRequest() and create a DataRequest.
  • Call newUpdatePersonRemoveDataRequest() in the generated DataRequest and generate a request.
  • Specify the owner of the application data (VIEWER cannot be changed) and the data key in the arguments.
  • Call DataRequest.add(request) and register the request.
  • Call DataRequest.send(callback) and make a request to acquire information.
  • Once the server request process is complete, the callback function specified with send() is executed using the DataResponse that contains the request results as an argument.
  • Process the data that is stored in DataResponse.

Restrictions on application data​

There are currently several restrictions on the use of application data:

  • Saving and deleting application data is restricted to the application data of the VIEWER.
  • Acquiring application data is restricted to the application data of the VIEWER and the VIEWER’s FRIENDS.
  • The size restrictions on keys and values are 32 bytes and 1024 bytes, respectively.
  • Key and value pairs are restricted to 30 per user with an installed application.

Revision History​

  • 12/2010
    • Initial Release.