Novice Android developers encounter some doubts during the development process. I hope the experts can help guide me.
For example, my App needs to create a sqlite database when it is first installed.
My solution is to execute this creation method when the launcher starts the page.
But I think creating a sqlite database is a time-consuming operation. I need to put it in a sub-thread for execution
Then the question arises:
If the creation of the database in the sub-thread fails, how can I ensure that the subsequent methods can be executed smoothly?
When I create the database, the launcher may have jumped to mainActivity, or has been transferred to other pages, or is even ready to write data into the database, but at this time I can determine whether the database Exists. If it does not exist, I can re-create it, but it is impossible for me to re-create the database in all places where database operations are required.
So, how can I ensure that the operation results in the sub-thread are 100% completed?
And there are several other operations in the launcher, all of which need to be completed in child threads.
Solution.
天蓬老师2017-05-24 11:40:12
1. Encapsulate it
2. For example, encapsulate a DBUtils layer.
bool checkDB(){
return ..;
}
Beans DBUtils.query(callback){
if(checkDb){
callback(data);
}
}
phpcn_u15822017-05-24 11:40:12
It is recommended to use strong blocking logic and pop up the non-cancelable loading box to ensure the execution result.
天蓬老师2017-05-24 11:40:12
It is recommended to refer to the fact that when mobile QQ is started for the first time, there is a progress bar such as data initialization.