Closed cfilipov closed 6 years ago
Thanks @cfilipov :-)
But... should we keep this issue open? RxGRDB can not ship with your code, because it is too particular. We don't provide tools that lock users in a particular application architecture. Instead, we ship versatile tools that fit in as many setups as possible.
I would like to share a pattern I am using to safely and easily wait for database initialization. I hope by sharing this it will help inform GRDB's design based on how it's being used and also catch anything that could be done by better utilizing GRDB's existing features.
I don't want any queries to execute before the db migration or loading of initial data set is complete. I also don't want to naively block the whole app while waiting. Without reactive programming this could get very messy. To accomplish this I wrap access to the db queue through an observable/signal so that the only way to get a queue is by observing the signal that creates it.
Now all db queries look something like this:
In the above, the query wont be executed until the database is ready. Additionally, since the input is also a signal, the query will be re-run when the input changes (like when significantTimeChange fires for example).
Some migrations may be fast, some larger migrations may take a while. A polite way to proceed is to only show a loading indicator if the migration takes "long enough". If the migration completes before the deadline no loading indicator should be shown. This will prevent the "quick flash" loading screen many apps have. The signal below is used in the root view controller to display a full screen loading indicator when a large migration takes place.