Rudiksz / couchbase_lite_dart

Dart implementation of the Couchbase Lite, an embedded, NoSQL JSON Document Style database.
BSD 3-Clause "New" or "Revised" License
24 stars 3 forks source link

Getting database Status(BUSY,etc....) #11

Closed richard457 closed 3 years ago

richard457 commented 3 years ago

use case: Having more than two apps that need to sync, and logging to another different device then you need to wait for the local database to sync with other databases "remote", this is more useful in connecting different apps for example web and mobile clients.

richard457 commented 3 years ago

For example, Querying the database at the wrong time will result in wrong data, and there is no way with the current solution to use something like this,

ProxyService.database.replicator
                                        .addChangeListener((status) {
                                      while (status.activityLevel.toString() ==
                                              'busy' ||
                                          status.activityLevel.toString() ==
                                              'idle') {
                                        log.i("busy syncing...");
                                        return;
                                      }
                                    });

I might be wrong

richard457 commented 3 years ago

Mostly useful when a user is logging into the system and he needs the system to get data from the remote before he proceeds with Querying the data.

Rudiksz commented 3 years ago

There's many ways to solve the issue of the initial replication, the easiest is to just set up live queries and show some kind of indicator if you don't have data and the replicator is still running.

Conceptually what you are trying to do here is to execute a query when your replicator finishes, but a live query does just that: if the data is changed it will be executed again.

Think of listeners in the reactive way, the function passed as an argument to the listener will be called every time replicator will changes status with the current snapshot. Using while inside of that function makes no sense because the value of status never changes. In a reactive context your function gets called again with the new value and you do the work you need based on the new value. Like updating your state in some StatefulWidget and calling setState (or whatever goes as setState in your state management approach).

Edit: to try clarify a bit more, think of adding a changeListener as setting up a giant while loop with the body being the function you pass to the changeListener. And the status being what changes between the different iterations.

The alternative (old fashioned) way is to use the replicator.status to poll the status, but that's quite inefficient.