Tutorial: Watch data acknowledgement

Tips and Tricks Watch data acknowledgement

How to watch the acknowledgement of some data

Usually, watching the acknowledgement of some data cannot consist in simply subscribing to the “Value Acknowledgement” and/or “Child Acknowledgement” events (see Subscribe to changes). Indeed, other raised events may convey data values that are already acknowledged (the corresponding received notifications provides a field or property to check it). In this case, such data will never raise a “Value Acknowledgement” or “Child Acknowledgement” event. Therefore, the right technique is to subscribe to both “Value Change” and “Value Acknowledgement” events, or to all of the child-related events (including “Child Acknowledgement”).

Suppose we want to share the title of a contact directory between several users and that each user is allowed to edit it anytime. When edited, we display it as long as it is not acknowledged by the Webcom back end, and we change its color to black as soon as it is acknowledged.

// Reference to the title of our contact directory
const ref = new Webcom("<your-app>");
const titleNode = ref.child("title");
// We track both "value" AND "value_ack"
titleNode.on(["value", "value_ack"], snapshot => displayTitle(snapshot.val(), snapshot.acknowledged()));

/**
 * Displays the given `title` in the acknowledged or not acknowledged state along the given `acknowledged` value.
 * @param {string} title
 * @param {boolean} acknowledged
 */
function displayTitle(title, acknowledged) {
    // some code
}
// Reference to the title of our contact directory
val app = WebcomApplication.default
val manager = app.datasyncService.createManager()
val titleNode = manager / "title"

// We track "ValueChanged.WithData" INCLUDING acknowledgements
val subscription = titleNode.subscribe(ValueChanged.WithData, includesAcknowledgements = true) {
    if (it is WebcomResult.Success) {
        val notification = it.result
        displayTitle(notification.value.convertTo(String::class).get(), notification.dataAcknowledged)
    }
}

fun displayTitle(title: String, acknowledged: Boolean) {
    // some code
}
let app = Webcom.defaultApplication
let manager = app.datasyncService.createManager()
let titleNode = manager / "title"

// We track value events INCLUDING acknowledgements
titleNode?.subscribe(to: .value(change: true, acknowledgement: true)) { event in
    displayTitle(title: event.value.asString, isAcknowledged: event.value.isAcknowledged)
}

func displayTitle(title: String?, isAcknowledged: Bool) {
    // some code
}

In this way, when a user changes the title on her/his device, it will be grayed until the back end acknowledges it (this may last if the device is offline). If another user on another device changes the title, then it will be updated directly in black on the first user's device (as the back end will notify it of an already acknowledged piece of data).

You can see a more detailed and more complex example, involving child-related events, here.