A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/node/react-to-changes/ below:

React to Changes - Node.js SDK - Atlas Device SDKs

Data in Realm is live, which means that an object always reflects its most recent saved state and read operations never block. Objects automatically update in response to changes, so you can see up-to-date data in your application without running a new query. Objects and queries emit notifications that can update your app whenever data changes.

You can register three types of notification listeners:

To register a change listener for an entire realm, pass a callback function to the realm's addListener() method. Realm calls the listener asynchronously whenever an operation adds, changes, or removes objects in the realm.

To remove a realm listener, pass the callback to the realm's removeListener() method.

Tip Use Object & Collection Listeners for Change Details

Realm does not pass any information about what changed to realm listener callback functions. If you need to know more information about what changed in an object or collection, use object listeners and collection listeners.

Tip Handling Exceptions Inside a Listener

To handle exceptions thrown from a change listener, wrap your addListener() call within a try...catch statement.

function onRealmChange() {  console.log("Something changed!");}try {  realm.addListener("change", onRealmChange);} catch (error) {  console.error(    `An exception was thrown within the change listener: ${error}`  );}realm.removeListener("change", onRealmChange);

To register a change listener for a collection of Realm objects, pass a callback function to the collection's addListener() method. Realm calls the listener asynchronously when it's registered as well as whenever an operation adds, changes, or removes objects in the collection.

To remove a collection listener, pass the callback to the collection's removeListener() method.

Important Order Matters

In collection notification handlers, always apply changes in the following order: deletions, insertions, then modifications. Handling insertions before deletions may result in unexpected behavior.

const dogs = realm.objects("Dog");function onDogsChange(dogs, changes) {    changes.deletions.forEach((index) => {            console.log(`Looks like Dog #${index} has left the realm.`);  });    changes.insertions.forEach((index) => {    const insertedDog = dogs[index];    console.log(`Welcome our new friend, ${insertedDog.name}!`);  });    changes.modifications.forEach((index) => {    const modifiedDog = dogs[index];    console.log(`Hey ${modifiedDog.name}, you look different!`);  });}try {  dogs.addListener(onDogsChange);} catch (error) {  console.error(    `An exception was thrown within the change listener: ${error}`  );}dogs.removeListener(onDogsChange);

To register a change listener on a specific Realm object, pass a callback function to the object's addListener() method. Realm calls the listener if any of the object's properties change or if someone deletes the object.

To remove an object listener, pass the callback to the object's removeListener() method.

function onDogChange(dog, changes) {  if (changes.deleted) {    console.log(`dog is deleted: ${changes.deleted}`);  } else {    changes.changedProperties.forEach((prop) => {      console.log(`* the value of "${prop}" changed to ${dog[prop]}`);    });  }}try {  dog.addListener(onDogChange);} catch (error) {  console.error(    `An exception was thrown within the change listener: ${error}`  );}dog.removeListener(onDogChange);

To remove all listeners on a given realm, object, or collection instance, call the instance's removeAllListeners() function:

realm.removeAllListeners();dogs.removeAllListeners();dog.removeAllListeners();

Changes in nested documents deeper than four levels down do not trigger change notifications.

If you have a data structure where you need to listen for changes five levels down or deeper, workarounds include:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4