realm-dart icon indicating copy to clipboard operation
realm-dart copied to clipboard

Support Flexible Sync Subscribe API

Open sync-by-unito[bot] opened this issue 1 year ago • 4 comments

sync-by-unito[bot] avatar Apr 19 '23 07:04 sync-by-unito[bot]

Are there plans to implement this?

dotjon0 avatar Oct 31 '23 00:10 dotjon0

Yes, we have a branch for it: https://github.com/realm/realm-dart/pull/1354. I'll need to review it and see what it's missing so that it can be merged.

nirinchev avatar Oct 31 '23 10:10 nirinchev

I believe there's already an implementation for this (I saw some PRs https://github.com/realm/realm-dart/pull/1354) but it seems faulty. I've been trying to implement it without any success, every time I try to subscribe it returns the following error:

Unhandled Exception: Null check operator used on a null value
#0      SubscriptionSet.waitForSynchronization (package:realm_dart/src/subscription.dart:166:18)
<asynchronous suspension>

The error originates from the following section, where error inside throw error!; is indeed null. The root of the error is _waitForStateChange, something fails inside but I don't know why since no meaningful message is returned, I tried to follow the flow but got to some pointers and that was the end of the road for me.

  Future<void> waitForSynchronization([CancellationToken? cancellationToken]) async {
    final result = await _waitForStateChange(SubscriptionSetState.complete, cancellationToken);
    if (result == SubscriptionSetState.error) {
      throw error!;
    }
  }

  /// Returns the error if the subscription set is in the [SubscriptionSetState.error] state.
  Exception? get error => realmCore.getSubscriptionSetError(this);

Here's my implementation:

var peersQuery = realm.query<UserPeer>('_id == oid(5d17121088fbfb008254c14a)');

final subscription = await peersQuery.subscribe(
  name: "peersQuery",
  waitForSyncMode: WaitForSyncMode.firstTime,
  cancellationToken: TimeoutCancellationToken(Duration(seconds: 60)),
  update: true,
);

Manual Subscriptions:

var peersQuery = realm.query<UserPeer>('_id == oid(5d17121088fbfb008254c14a)');

realm.subscriptions.update((MutableSubscriptionSet mutableSubscriptions) {
  mutableSubscriptions.add(peersQuery, name: "peersQuery");
});

await realm.subscriptions.waitForSynchronization();

Both fail, I've tried different queries, .all() query and everything returns the same error, am I missing something? Thanks!

HSCOGT avatar Mar 10 '24 01:03 HSCOGT

Can you post trace-level logs from a run that fails?

nirinchev avatar Mar 10 '24 01:03 nirinchev