android - Room - when to initialize database and how to access it through app lifecycle -
i'm trying set room way simplify access sqlite database. i've written code, can't run it, app throws following exception:
cannot access database on main thread since may potentially lock ui long period of time
i've done research , i've found .allowmainthreadqueries() in databasebuilder seems me terrible solution, because it's muting error message, not fixing real cause.
so, best practices? when (in app lifecycle) should create database , should store access activity want?
when (in app lifecycle) should create database
lazy-create roomdatabase on first access, when using sqliteopenhelper directly.
where should store it
a singleton typical pattern, when using sqliteopenhelper directly.
neither of questions have error message. matter of accessing database on background thread, when using sqliteopenhelper directly. @query methods, have option of having method return livedata or rxjava type (e.g., flowable), in case room take care of doing work on background thread. other operations (e.g., @insert), responsible invoking methods on background thread (thread, asynctask, threadpoolexecutor, intentservice, jobintentservice, etc.).
Comments
Post a Comment