tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

Issue using Sembast in Background Notification - Android #356

Open jamescardona11 opened 1 year ago

jamescardona11 commented 1 year ago

Hello, I hope everything is going well.

I have a question; I have a Sembast instance in foreground state to watch all information from the database. When I change the app background state and receive a notification from Firebase, I save some data in Sembast, I saw in the documentation Firebase Background Notification works like an Isolate. The data is saved to Sembast with that, but the main app can't emit that new data received from the notification background. If I continue sending information in foreground, the main isolate is emitted data from Sembast.

When I do a HotReload, the data from background is emitted. I want to drop a video to show the behavior that only happens in Android.

I tried to stop and restart the subscription in the main Isolate, and is not emitting data that is received from the notification background.

https://user-images.githubusercontent.com/65878592/233739662-fa97b992-13d8-48ce-999c-ecb171861177.mp4

alextekartik commented 1 year ago

Unfortunately I don't have a good solution. sembast is definitely not safe in background isolate. I would personally use a separate solution for the notification:

jamescardona11 commented 1 year ago

Unfortunately, creating a second table and reading from there when the app returns to the foreground doesn't work; In some cases everything seems to work well, in other moments I read the background table and receive nothing.

I'll try another solution and leave my answer here.

When you said read mode, is it something special?

x3rud commented 1 year ago

@jamescardona11 did you find any solution for that? I have the same problem

jamescardona11 commented 1 year ago

@x3rud no directly with Sembast; I made a mix following the suggestion from Alex about writing in a temporal file and read/write when the app is open again.

x3rud commented 1 year ago

Thank you. The strange thing is that the data is being stored on the database.

@alextekartik have you thought about a hypothetical solution in recent months?

alextekartik commented 1 year ago

The issue is that since every isolate has its own copy in memory content might be different with what is store in the database. I don't have a good solution for this unfortunately...sorry.