subscribeSnapshot method Null safety

Future<Stream<AGConnectCloudDBZoneSnapshot?>> subscribeSnapshot(
  1. {required AGConnectCloudDBQuery query,
  2. required AGConnectCloudDBZoneQueryPolicy policy}
)

You can call this method to register a listener for a specified object.

When the object that is listened on is changed, the stream is triggered. After a snapshot listener is registered, the snapshot listener takes effect after a certain period of time (limited by network status). Therefore, a snapshot before the listener takes effect is not sent to the device. After the listener takes effect, the device can receive the snapshot. If the size of the snapshot exceeds 5 MB or the number of data records contained in the snapshot exceeds 1000, the snapshot cannot be sent to the device.

Comply with the following rules when invoking this method:

  • You can call this method to register a listener only when Cloud DB zone is opened. Otherwise, the listener fails to be registered.
  • You can call this method to register a listener only when the Cloud DB zone synchronization mode is cache mode.
  • When registering a listener, equalTo() supports the following field data types:
    • bool for Boolean.
    • int for Byte, Short, Integer, Long, and Date.
    • double for Double, and Float.
    • String for String.

query

  • Only the equal-value subscription, that is, the equalTo() method can be used to construct query conditions. The query conditions must contain at least one field and at most five fields. The AND operation is used between multiple query conditions.

policy

  • Query policy, which specifies the data source to be queried. When a query policy is specified, the data source can only be POLICY_QUERY_FROM_CLOUD_ONLY or POLICY_QUERY_DEFAULT.

Implementation

Future<Stream<AGConnectCloudDBZoneSnapshot?>> subscribeSnapshot({
  required AGConnectCloudDBQuery query,
  required AGConnectCloudDBZoneQueryPolicy policy,
}) async {
  try {
    String channelId;
    do {
      channelId = '';
      for (int i = 0; i < 8; i++) {
        channelId += Random.secure().nextInt(10).toString();
      }
    } while (_activeSnapshotSubscriptionIds.contains(channelId));
    _activeSnapshotSubscriptionIds.add(channelId);

    final String eventChannelName =
        'com.huawei.agconnectclouddb/eventChannel/subscribeSnapshot/$channelId';
    final EventChannel eventChannel = EventChannel(eventChannelName);

    await _methodChannel.invokeMethod<void>(
      _MethodConstants.SUBSCRIBE_SNAPSHOT,
      <String, dynamic>{
        _KeyConstants.ZONE_ID: _id,
        _KeyConstants.QUERY: query.query,
        _KeyConstants.POLICY_INDEX: policy.index,
        _KeyConstants.EVENT_CHANNEL_NAME: eventChannelName,
      },
    );
    return eventChannel.receiveBroadcastStream().map((dynamic s) {
      return s is Map<dynamic, dynamic>
          ? AGConnectCloudDBZoneSnapshot._fromMap(
              Map<String, dynamic>.from(s))
          : null;
    });
  } catch (e) {
    throw AGConnectCloudDBException._from(e);
  }
}