---
title: Observing Data Changes
slug: v4-8/crud/observing-data-changes
docTags: 
createdAt: 2024-07-31T23:36:09.836Z
---

A *store observer* is a DQL query that runs continuously and triggers a callback when a change to the store impacts the results of the query.&#x20;

Store observers are useful when you want to monitor changes from your local Ditto store and react to them immediately. For instance, when your end user updates their profile, asynchronously display changes in realtime.

## Setting Up Store Observers

Using the `registerObserver` method, set up an observer within the `store` namespace enclosed with a query that specifies the collection to watch for changes, as well as your logic to handle the incoming changes. &#x20;

:::CodeblockTabs
```swift
let observer = ditto.store.registerObserver(
  query: "SELECT * FROM cars"){ result in /* handle change */ };
```

```kotlin
val observer = ditto.store.registerObserver("SELECT * FROM cars") { result ->
  /* handle change */ };
```

```javascript
const changeHandler = (result) => {
  // handle change
}
const observer = ditto.store.registerObserver(
  "SELECT * FROM cars",
  changeHandler);
```

```java
DittoStoreObserver observer = ditto.store.registerObserver(
    "SELECT * FROM cars",
    result -> {
        // handle change
    }
);
```

```csharp
// Without Arguments
var result = await ditto.Store.RegisterObserver(
  "SELECT * FROM cars",
  (result) => {
    // handle change
  });

// With Arguments
var result = ditto.Store.RegisterObserver(
  "SELECT * FROM cars",
  (result) => {
    // handle change
  });

```

```cpp
auto observer = ditto.get_store().register_observer(
  "SELECT * FROM cars",
  [&](QueryResult result) { /* handle change */ });
```

```rust
let observer = ditto.store().register_observer(
  "SELECT * from cars",
  None,
  move |result: QueryResult| {
    // handle change
  })
```

```dart
final observer = await ditto.store.registerObserver("SELECT * FROM cars");

observer.changes.listen((result) {
  // handle change
});

// or 

final observer = await ditto.store.registerObserver("SELECT * FROM cars",
  onChange: (result) {
    // handle change
});
```
:::

### Store Observer with Query Arguments

o associate arguments with your query add them as a paramater.

:::CodeblockTabs
```swift
let observer = ditto.store.registerObserver(
  query: "SELECT * FROM cars WHERE color = :color",
  argumets: [ "color": "blue" ]){ result in /* handle change */ };
```

```kotlin
ditto.store.registerObserver(
  "SELECT * FROM cars WHERE color = :color",
  mapOf("color" to "blue")) { result ->
  /* handle change */ };
```

```javascript
const changeHandler = (result) => {
  // handle change
}
const observer = ditto.store.registerObserver(
  "SELECT * FROM cars WHERE color = :color",
  changeHandler,
  { color: 'blue' });
```

```java
DittoStoreObserver observer = ditto.store.registerObserver(
    "SELECT * FROM cars WHERE color = :color",
    Collections.singletonMap("color", "blue"),
    result -> {
        // handle change
    }
);
```

```csharp
// Without Arguments
var result = await ditto.Store.RegisterObserver(
  "SELECT * FROM cars",
  (result) => {
    // handle change
  });

// With Arguments
var result = ditto.Store.RegisterObserver(
  "SELECT * FROM cars WHERE color = :color",
  new Dictionary<string, object> { "color", "blue" },
  (result) => {
    // handle change
  });

```

```cpp
auto observer = ditto.get_store().register_observer(
  "SELECT * FROM cars",
  {{"color", "blue"}}),
  [&](QueryResult result) { /* handle change */ });
```

```rust
struct Args {
    color: String,
}

//...

let args = Args {
    color: "blue".to_string(),
}

let observer = ditto.store().register_observer(
  "SELECT * from cars WHERE color = :color",
  args,
  move |result: QueryResult| {
    // handle change
  })
```

```dart
final observer = ditto.store.registerObserver(
  "SELECT * FROM cars WHERE color = :color",
  arguments: { color: "blue" },
  onChange: (queryResult) {
    // handle change
  },
);
```
:::

## Canceling a Store Observer

To cancel a store observer, call `cancel` on the observer object.

Once canceled, the store observer will stop processing in the background and will no longer call the provided callback.

:::CodeblockTabs
```swift
observer.cancel()
```

```kotlin
observer.cancel()
```

```javascript
observer.cancel()
```

```java
observer.cancel();
```

```csharp
observer.Cancel()
```

```cpp
observer.cancel();
```

```rust
observer.cancel()
```

```dart
await observer.cancel();
```
:::

## Accessing Store Observers

To access store observers from the local Ditto store:

:::CodeblockTabs
```swift
ditto.store.observers
```

```kotlin
ditto.store.observers
```

```javascript
ditto.store.observers
```

```java
ditto.store.observers;
```

```csharp
ditto.Store.Observers
```

```cpp
ditto.get_store().observers()
```

```rust
ditto.store().observers()
```

```dart
// Coming Soon
```
:::

