skip to Main Content

I am very close to finishing my OBX Sync project, but i need some answers to finish it.

  • How can i restart sync? (for example the server IP address changes in the background, ). Currently i have to restart the whole app, to restart sync.
  • How can i determine, when OBX finished sync on login? I will control the UI, and let show / control the user that all data are up-to-date / sync in progress. For example: user A went on vacation, and in the meantime his permissions are revoked. Since the user’s device was not connected to the server, these changes were not synced locally. When the user logs in again, during the synchronization the user’s data may be synchronized later than when he logged in, so he can log in even if he no longer exists on the server.
    Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Code for start sync is:

    startSync(BuildContext context) async {
      bool available = Sync.isAvailable();
      syncServerIP = '';
    
      if (available) {
        syncClient = Sync.client(objectBox!.store, 'ws://$syncServerIP', SyncCredentials.none());
        syncClient!.start();
        syncClient!.requestUpdates(subscribeForFuturePushes: true);
        final loginsubscription = syncClient!.loginEvents.listen((SyncLoginEvent event) {
          print('Login event: $event');
        });
        final changeSubscription = syncClient!.changeEvents.listen((List<SyncChange> event) => event.forEach((change) {
          debugPrint('${change.entity} '
                  'puts=${change.puts} removals=${change.removals}');
        })).onDone(() {
          print('Sync done');
        });
        final connectionSubscription = syncClient!.connectionEvents.listen((SyncConnectionEvent event) {
          switch (event) {
            case SyncConnectionEvent.connected:
              print('Connected to Sync Server');
              syncClient!.requestUpdates(subscribeForFuturePushes: true);
              break;
            case SyncConnectionEvent.disconnected:
              print('Disconnected from Sync Server');
              break;
          }
        });
        final completionSubscription = syncClient!.completionEvents.listen((event) {
          print('Sync event fired');
        });
      } else {
        print('Sync not available');
      }
    }
    

    the debug messages on start the app after startSync() started:

    I/flutter ( 6484): ProductGroup puts=[] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): Product puts=[] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableMapElement puts=[46, 49, 51, 52, 55, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 85, 94, 97] removals=[]
    I/flutter ( 6484): TableMap puts=[] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableMapElement puts=[] removals=[46, 49, 51, 52, 55, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 85, 94, 97]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[82] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[83] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[84] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[85] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[86] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[87] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[88] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[89] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[90] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): TableGUID puts=[91] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[1] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[2] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[3] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[4] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[6] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[7] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[9] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[10] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[12] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[13] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[14] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[15] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[16] removals=[]
    I/flutter ( 6484): Sync event fired
    I/flutter ( 6484): ProductGroup puts=[17] removals=[]
    I/flutter ( 6484): Sync event fired
    ...
    and so on...
    

    As you can see, the event runs after each data sync (After each piece of data, you see "Sync event fired", which, if you look at the code, is in the completionEvent.listen procedure. In this case, how can i be sure, if sync is finished, or still running?


  2. To restart a SyncClient after a configuration change, please close it and create a new instance. Assuming you have an existing SyncClient instance called syncClient, it should look something like this:

    syncClient.close();  // Important to close first...
    syncClient = Sync.client( /* new config here */ );
    syncClient.start()
    

    For the second question, I think there are some listeners mentioned in the docs:

    • loginEvents: such as logged-in, credentials-rejected
    • completionEvents: observe when synchronization has completed
    • connectionEvents: observe connection events

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search