Skip to content

Latest commit

 

History

History
130 lines (94 loc) · 2.78 KB

File metadata and controls

130 lines (94 loc) · 2.78 KB

Data Sync API Reference for Java

New object creation

using sync()

SyncedObject home = pubnub.sync("home");
SyncedObject thermostat = pubnub.sync("home.living_room.thermostat");
SyncedObject occupants = pubnub.sync("home.occupants");

using child()

SyncedObject home = pubnub.sync("home");
SyncedObject thermostat = home.child("living_room.thermostat");
SyncedObject occupants = home.child("occupants");

Value getters

If value doesn't exist or cannot be cast to getter's type, null will be returned.

getString()

String state = thermostat.getString("state");
// state is a string "on"

getInteger()

Integer temperature = thermostat.getInteger("temperature");
// temperature is an integer 68

getBoolean()

Boolean connected = thermostat.getBoolean("connected");
// temperature is a boolean true

getInteger() on string value

Integer state = thermostat.getInteger("state");
// state is null

getMap()

HashMap stateMap = home.getMap();

getList()

ArrayList occupantsList = home.getList("occupants");
ArrayList occupantsList = occupants.getList();

Mutate data methods

Merge

// merge(), unlike replace(), will add data to your object WITHOUT truncating existing child data.
JSONObject scoresUpdate = new JSONObject();
scoresUpdate.put("Chauncy", 10);

scores.merge(scoresUpdate);

// or without instantiating SyncedObject
Hashtable<String, Object> args = new Hashtable<String, Object>();

args.put("location", "scores");
args.put("data", scoresUpdate);

pubnub.merge(args, new Callback());

Merge

// replace(), unlike merge(), will add data to your object, WHILE truncating existing child data.
JSONObject scoresUpdate = new JSONObject();
scoresUpdate.put("Scotty", 3);

scores.replace(scoresUpdate);

// or without instantiating SyncedObject
Hashtable<String, Object> args = new Hashtable<String, Object>();

args.put("location", "scores");
args.put("data", scoresUpdate);

pubnub.replace(args, new Callback());

Remove

// remove() deletes data
scores.remove();

// or without instantiating SyncedObject
Hashtable<String, Object> args = new Hashtable<String, Object>();

args.put("location", "scores");

pubnub.remove(args, new Callback());

Push

// push() appends data to the end of a list container
JSONObject playersUpdate = new JSONObject();
JSONObject player1 = new JSONObject();

player1.put("name", "Randy");
player1.put("weapon", "dagger");

playersUpdate.put("Player_1", player1);

players.push(playersUpdate);

// or without instantiating SyncedObject
Hashtable<String, Object> args = new Hashtable<String, Object>();

args.put("location", "players");
args.put("data", scoresUpdate);

pubnub.push(args, new Callback());