Is there a way to automatically sync related model along with the synced model?

Hi, I’m currently developing a chat app. My data are stored in mongodb and I want to use Flexible Sync to selectively sync chats data to local. My code is something like below

class $Chat {
  @PrimaryKey()
  @MapTo('_id')
  ObjectId? id;
  ...
  String? channel;

  late List<$Media> medias;
}

class $Media {
  @PrimaryKey()
  @MapTo('_id')
  ObjectId? id;
  ...
}

 realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions.add(realm.query<Chat>(r' channel = 1 '));
});

As you can see above there is a relationship between chat and media. What I want is when chats are synced I also want each media (of synced chats only) to be synced as well.

  • Is there a way to automatically sync media along with the chat?
  • Or do I need to manually add a query subscription for each media? If I need to do it manually, there will many media subscriptions for each chat. Is there a subscription limitation when too many subscriptions are added?

Unfortunately not. This is a well known and to be honest embarrassing limitation that we are working on lifting. For now you basically need to add an extra “foreign-key” relation-ship between $Media and $Chat and use that in your subscription query. Fx. by adding channel to $Media as well, so you can do:

final channel = 'awesome channel';
realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions
        ..add(realm.query<Chat>(r'channel = $0', [channel]))
        ..add(realm.query<Media>(r'channel = $0', [channel]));
});

I would not recommend doing a query per $Media.