0

I am using android_alarm_manager_plus to run a dart isolate. In that isolate, I modify some data and I want those changes to reflect in the main isolate. Likewise, I want changes made in the main isolate to reflect in the alarm isloate.

What I have tried

Currenlty I am using get_storage to save and read data. But it isn't synchronizing between isolates.

Alarm isolate

await GetStorage.init();

// Some time later
GetStorage().write("data", data);

Main Isolate

await GetStorage.init();

// This read always occurs after the write in the alarm isolate, but it still doesn't reflect the changes made in that write.
GetStorage().read("data");

Before this, I was using shared_preferences to do the same. shared_preferences actually had a reload method which was supposed to synchronize from disk, but I am not sure how to use it in my use case.

I have also tried the ugly method of writing and reading data to a file manually. This works, but is probably slow and I have to make sure that writes don't overlap (I would prefer not to use synchronous writes since it will block the UI).

Ahsan Sarwar
  • 23
  • 1
  • 4
  • 1
    It's impossible to share memory across isolates, since each isolate has its own memory. This is intentional and by design, as shared memory is very error-prone. If you want to have a system that synchronizes data across isolates, you would need to create that system yourself using the `SendPort` and `ReceivePort` of each isolate communicating back to a central place that propagates changes out to all the other ones. It will not be straightforward, and you will run into that error-prone issue. – Abion47 Feb 28 '23 at 04:40
  • @Abion47 But is it also a bad idea to share storage between them? – Ahsan Sarwar Feb 28 '23 at 04:51
  • Storage is just another name for memory (in this context), so yes. – Abion47 Feb 28 '23 at 04:56

0 Answers0