Skip to content
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

How to know when callback are finished ? #264

Closed
istornz opened this issue Feb 19, 2020 · 4 comments
Closed

How to know when callback are finished ? #264

istornz opened this issue Feb 19, 2020 · 4 comments

Comments

@istornz
Copy link

istornz commented Feb 19, 2020

Hello,
I want to know when the callback is finished. I try to populated the DB but I can't know when the populating process is finished ...

final callback = Callback(onCreate: (database, version) {  }, onOpen: (database) {
      // How to know when this is finished ?
      });
    });

I tried to add "async" on onOpen func but it doesn't work ...

final callback = Callback(onCreate: (database, version) {  }, onOpen: (database)  async {
     final String = await [...]; // It's doesn't work :/
      });
    });

Any help would be greatly appreciated :)

@vitusortner
Copy link
Collaborator

Hi! Could you describe your use case a bit more and tell me exactly what's not working? Thanks in advance.

@istornz
Copy link
Author

istornz commented Feb 19, 2020

Hi @vitusortner !
Thanks for your reply !

So, I want to pre-populate the database at the first startup (in order to inject static data).
To do that:

  1. Created a Singleton to keep the reference of the database across my app (without pass it to all my widgets).
  2. I added a callback and assign it on the "onCreate" state. This callback call a transactional queries (delete all & insert a list of objects).
  3. The main problem is: I don't know when the transactional operation is finished (or not). So when I try to call the instance in other widget, data are not present (because the insert is not terminated). When I start again the app, it works.

Thanks for your time & your awesome lib !

@istornz
Copy link
Author

istornz commented Feb 19, 2020

I managed to pass around this "issue" by hooking the initialization process:

MyFlutterDB initiatedDb = await $FloorMyFlutterDB.databaseBuilder('cache.db').build();
await _setupDB(initiatedDb); // Here i pre-populate my database
return initiatedDb; // Return the initiated db AFTER the setup

Note: I use a Singleton pattern and it works just fine.

@vitusortner
Copy link
Collaborator

Callback's functions work asynchronously. This means that they will be invoked sometime later. blocking their execution by adding async and await won't make the event looper stop until the initialization is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants