Automatically purge a collection

Hello, I’m currently new to Realm and I’m building an app for offline functionality with clinical histories. As more patients are treated, this collection will grow larger. Therefore, I would like to have a mechanism to have on the mobile device only the information that has changed in the last week. This is to have a small database on my device and avoid having too much information in the app when it is offline.

Hi @Fabian_Eduardo_Diaz_Lizcano!
Wellcome the MongoDB forum!
Yes, we have such mechanism using realm and sync to Atlas MongoDB - https://realm.mongodb.com.
I assume you have already an AppService at Atlas MongoDB cloud. And here is some simple code how to connect from a flutter app after you have added the realm | Flutter Package.

final app = App(AppConfiguration("app-service-id"));
final user = await app.logIn(Credentials.anonymous());
final realm = Realm(Configuration.flexibleSync(user, [Card.schema]));

The data downloaded to the device are defined using subscription sets. You can define a subscription with a filer by date. And you can re-create this subscription on daily bases using a timer or some other approach. The filtered data will be downloaded automatically and if you have other items matching the criteria during the whole day they will be automatically downloaded.

  realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions.removeByName("dateFilter");
    mutableSubscriptions.add(realm.query<Card>(r"date >= $0", [today]), name: "dateFilter");
  });
  await realm.subscriptions.waitForSynchronization();
  await realm.syncSession.waitForDownload();
  realm.write(() => realm.add(Card(ObjectId(), DateTime.now())));

If you want to control the online/offline mode you can use:

   realm.syncSession.resume(); // go online
   //re-create the subscription and wait for download
   realm.syncSession.pause(); // go offline

Feel free to ask if you have any further questions.

Hi @Desislava_St_Stefanova

Thank you for your response.

I have a subscription as you mentioned and I see that the offline topic works, but after the time that I define in the subscription, I still see the data on the device where I created the entity. I would like this information to be automatically deleted from the device to have only the most recent information, since the collections that I am going to create will have information that once the process is finished I will only read for reports.

For this reason, I would like to gradually delete this information from mobile devices only.

Thanks in advance,

Hi @Fabian_Eduardo_Diaz_Lizcano!
The synced Realm contains only the data matching the subscriptions, all the other realm objects are deleted automatically. If you add new subscriptions the data matching all the subscriptions will exist on the device. You have too be sure that you have deleted the old subscriptions before to add a new one as it is in the sample above. Or you can use named subscription with update flag. Then each time a subscription with the same name is added the query will be replaced. Be sure to do this when the device and realm syncSession are online.

realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions.add(realm.query<Card>(r"date >= $0", [today]), name: "dateFilter", update: true);
  });

If your don’t see the objects in the Realm but your Realm file size continue to grow, you should know that
the file size will be reduced automatically later. But if you want to shrink it immediately then you can force the compaction. See https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/realm-files/compact/#std-label-flutter-compact.

Please, let me know if you still see the old realm objects. If yes, it will be nice if you can share a part of your code.

2 Likes

Hi @Desislava_St_Stefanova

Thank you very much for your help with the flat update:true I see that I have the expected behavior.

2 Likes