-
-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there anyway to access store from inside of an Isolate in flutter? #93
Comments
At first, the issue you get has nothing to do with sembast. As stated in the warning make sure you call That is said. I would not recommend using sembast in a isolate. Sembast is not cross isolate safe so basically you would not even be able to read from your main isolate.... Also I don't think you would get any speed benefit. |
I already did this
I still get the same exception. I also want to be able to sync data using service, would that be possible? Edit: |
Weird indeed...Not sure what you mean by service but i think my response will likely be: no ;) you have to use sembast in a single isolate. |
Yes very weird. Service as in android native services. Alright then, thank you for your quick responses and help. |
@shashikantx what you saw had nothing to do with Sembast apparently, it's a limitation of Flutter: isolates are not built to use platform channels. While the warning of Alex about Sembast not being isolate-safe is important to heed, if you want to work around that limitation in other scenarios, consider using https://pub.dev/packages/isolate_handler . I use it for something else with success. |
@deakjahn hi, Yes, thank you, I figured that out earlier, Also another issue I have run into while using isolate_handler is once I initiate Sembast from isolate, I cannot access db from main isolate. Flutter is kinda hard to make background tasks make work. |
That I wouldn't know, I only started using Sembast two days ago :-), I was on Sqflite earlier and I'm making the move now. So my isolate platform communications are in a different department, doing uploading of larger files and isolate_handler works wonders there. |
How about initiating sembast on each isolate :-D For example when I wanna save my data
Also, I init my Sembast instance in the mainstream separately on the splash screen as a static variable ... :-D I think as long as we use one of the Sembast instances (main or isolate) at a time there won't be a problem (especially when we are writing on different collections) But I think when we have simultaneous read/writes in the same collection there might be a risk of bug using my method (an isolate for each read/writes with a new Sembast instance in that isolate) :-D. @alextekartik As the developer of this plugin, what do you say? am I right? :-D Well at least it worked for me ( the writing part ) Now I'm gonna use the same strategy for reading :-D Update: Reading also worked with above strategy |
@lvlrSajjad You will also need a cross isolate mutex/lock to ensure that open/do_action/close are not ran at the same time. Performance will be poor and you cannot keep your database open in the main isolate (changes from the other isolates will be ignored). Currently I don't see an easy solution (besides having a single isolate handling database access and other isolates "talking" with this isolate). |
@alextekartik I agree with your last solution. Having a single isolate for the database is a good solution. |
Finally, I decided to use Sembast in the main isolate (Like normal) and use isolates for pre/post data processing instead. Because there were issues even with a single isolate (other than the main isolate). |
@lvlrSajjad Good choice! Running from another isolate is not tested/recommended at this point. |
I have been trying to implement the data sync in flutter app, but doing it from future is very inefficient, So I thought I could use isolates to do so, but the app fails to run throwing this exception
this is the code isolate has
Can I do anything to make it work ? Or is there a way to optimize this process?
The text was updated successfully, but these errors were encountered: