Tutorial: Browse data

Serverless DatabaseData Structure Browse data

Before doing any operation on a Webcom database, you need a node reference, which provides a pointer to a location or a data node inside your database tree.

Creating a node reference is an extremely light-weight operation, so you can create as many as you like without worrying about wasting bandwidth or memory.

To take a reference to the root node of the specified Webcom application:

var sampleChatRef = new Webcom("samplechat");

Node references then come with a couple of methods to easily navigate through the tree-like structure of the data.

child() method

First, each reference provides a method that takes the relative path to a child and returns a reference pointing to this child node:

var childRef = sampleChatRef.child("users");

The child() method accepts several path segments separated by slashes ("/"), as well as usual "." and ".." segments to browse relatively within the data tree hierarchy:

var johnRef = sampleChatRef.child("users/john");
// is equivalent to:
var otherRef = sampleChatRef.child("users").child("john");
// and also equivalent to:
var yetAnotherRef = sampleChatRef.child("users/mary/../john");

parent() method

Second, each reference provides a method that returns a reference to its parent node, or null if the reference already points to the root data node:

var parentRef = childRef.parent();
// 'parentRef' and 'sampleChatRef' now point to the same node.

Examples

Here are several ways of creating a reference that points to the same Webcom data node:

var usersRef = sampleChatRef.child("users");
var fredRef = usersRef.child("fred");
// is equivalent to:
var fredRef = sampleChatRef.child("users/fred");

var messageListRef = fredRef.parent().parent().child("message_list");
// is equivalent to:
var messageListRef = new Webcom("samplechat").child("message_list");
// and to:
var messageListRef = fredRef.child("../../message_list");

Note that the behaviors of the child and parent methods differ when applied on the reference pointing to the root data node:

sampleChatRef.child("..");
// => returns 'sampleChatRef'
sampleChatRef.parent();
// => returns 'null'

To take a reference to the root node of the specified Webcom application:

Webcom sampleChatRef = new Webcom(new WebcomApp("samplechat"));

Node references then come with a couple of methods to easily navigate through the tree-like structure of the data.

child() method

First, each reference provides a method that takes the relative path to a child and returns a reference pointing to this child node:

Webcom childRef = sampleChatRef.child("users");

The child() method accepts several path segments separated by slashes ("/"), as well as usual "." and ".." segments to browse relatively within the data tree hierarchy:

Webcom johnRef = sampleChatRef.child("users/john");
// is equivalent to:
Webcom otherRef = sampleChatRef.child("users").child("john");
// and also equivalent to:
Webcom yetAnotherRef = sampleChatRef.child("users/mary/../john");

parent() method

Second, each reference provides a method that returns a reference to its parent node, or null if the reference already points to the root data node:

Webcom parentRef = childRef.parent();
// 'parentRef' and 'sampleChatRef's now point to the same node.

Examples

Here are several ways of creating a reference that points to the same Webcom data node:

Webcom usersRef = sampleChatRef.child("users");
Webcom fredRef = usersRef.child("fred");
// is equivalent to:
Webcom fredRef = sampleChatRef.child("users/fred");

Webcom messageListRef = fredRef.parent().parent().child("message_list");
// is equivalent to:
Webcom messageListRef = new Webcom(new WebcomApp("samplechat")).child("message_list");
// and to:
Webcom messageListRef = fredRef.child("../../message_list");

Note that the behaviors of the child and parent methods differ when applied on the reference pointing to the root data node:

sampleChatRef.child("..");
// => returns 'sampleChatRef'
sampleChatRef.parent();
// => returns 'null'

WebcomApplication instance

First, you need to take a reference to the Webcom application.

The easiest way to configure it is to add a Webcom property in the Info.plist file of your iOS application as follows:

Key Type Value Description
▼ Webcom Dictionary root key for Webcom parameters
      identifier String samplechat identifier of the Webcom application

Then you can use this pretty shortcut:

let application = WebcomApplication.default

DatasyncManager instance

Second, you need to instantiate a DatasyncManager object. It manages a collection of node references and will be specially useful to read data.

let datasyncManager = application.datasyncService.createManager()

Usually, you directly write:

let datasyncManager = WebcomApplication.default.datasyncService.createManager()

node(for:) method and / operator

Finally, you get a node reference using its absolute path:

let rootNode = datasyncManager.node(for: "/") // The initial "/" is optional.
let usersNode = datasyncManager.node(for: "/users")

or:

let rootNode = datasyncManager / ""
let usersNode = datasyncManager / "users"

The node(for:) method accepts several path segments separated by slashes ("/"), as well as usual "." and ".." segments to browse relatively within the data tree hierarchy:

let johnNode = datasyncManager.node(for: "users/mary/../john")
// is equivalent to:
let otherNode = datasyncManager.node(for: "users/mary/../john")

or:

let johnNode = datasyncManager / "users/john"
// is equivalent to:
let otherNode = datasyncManager / "users/mary/../john"

relativeNode(for:) method and / operator

You can also get a node reference using a path relative to another node reference:

let alsoJohnNode = usersNode?.relativeNode(for: "john")
// 'alsoJohnNode' and 'johnNode' now point to the same node.

or:

let alsoJohnNode = usersNode / "john"
// 'alsoJohnNode' and 'johnNode' now point to the same node.

parent property

Each reference provides a property that returns a reference to its parent node, or nil if the reference already points to the root data node:

let parentNode = usersNode?.parent
// 'parentNode' and 'rootNode' now point to the same node.

Examples

Here are several ways of creating a reference that points to the same Webcom data node:

let usersNode = datasyncManager / "users"
let fredNode = usersNodes / "fred"
// is equivalent to:
let fredNode = datasyncManager / "users/fred"

let messageListNode = fredNode?.parent?.parent?.relativeNode(for: "message_list")
// is equivalent to:
let messageListNode = WebcomApplication.default.datasyncService.createManager().node(for: "message_list")
// and to:
let messageListNode = fredNode?.relativeNode(for: "../../message_list")

Note that the behaviors of the relativeNode(for:) method and the parent property differ when applied on the reference pointing to the root data node:

rootNode?.relativeNode(for: "..")
// => returns 'rootNode'
rootNode?.parent
// => returns 'nil'

Once you have a node reference, you can use it to write, read and query the corresponding data node within your data tree using the other Webcom SDK functions.