Skip to main content

Reactivity

One of the key features of QuantumCacheDB is its reactivity. It allows your application to automatically update the UI or perform actions in response to data changes. This is achieved through streams and an event-driven architecture that makes it easy to keep your application in sync with your database.

Key Features of Reactivity

  • Reactive Data Updates: Automatically listen for and respond to changes in your database.
  • Streams: Use Dart Streams to get notified of updates in real-time.
  • Real-time Synchronization: Data changes in the database trigger updates in the application, ensuring that the UI or other parts of your application are always up to date.
  • Event-Driven: QuantumCacheDB relies on an event-driven model, ensuring efficient communication between the database and application.

How Reactivity Works

In QuantumCacheDB, data updates are pushed to the application via streams. When you modify the database, the change is broadcasted to all listeners, and any active UI components or business logic dependent on the data are automatically updated.

Example Usage

Here’s how you can set up reactivity in QuantumCacheDB.

  1. Set up the database:

    First, initialize your database as usual:

    import 'package:quantum_cache_db/quantum_cache_db.dart';

    void main() async {
    var db = QuantumCacheDB();
    await db.init();

    // Now, you can proceed to work with the database
    }
  2. Use Streams to listen for updates:

    You can create a stream to listen to changes in the database. This stream will emit updates whenever the data changes.

    // Listening for changes in a collection
    var stream = db.collection("users").watch();

    stream.listen((event) {
    print("Data changed: ${event.data}");
    });

    In this example, the stream listens to changes in the users collection, and whenever any data changes, the listener is notified.

  3. Update Data and React:

    Once you’ve set up a listener, any changes you make in the database will automatically trigger the stream and update the application. For example:

    await db.collection("users").doc("user_123").set({
    "name": "John Doe",
    "email": "john@example.com"
    });

    The above code will trigger the listener and print the updated data as a response to the change.

Advanced Reactivity: Querying with Streams

You can also set up more advanced reactivity with queries. This allows you to subscribe to specific conditions and get updates only when the conditions are met.

Example of Reactive Query:

var stream = db.collection("users").where("age", ">", 18).watch();

stream.listen((event) {
print("Users above 18: ${event.data}");
});

In this example, the stream will only emit updates when a user’s age is above 18.

Benefits of Reactivity

  • Automatic UI Updates: You don't need to manually update the UI whenever data changes. The stream will handle this for you.
  • Efficient Data Handling: Streams ensure that your application only listens for necessary updates, reducing overhead.
  • Real-Time Applications: Ideal for building real-time applications where the data constantly changes (e.g., messaging apps, dashboards).