我在版本1.3.1中使用Sugar ORM作为我的Android 5.1应用程序。
今天,我注意到在安装我的应用程序后,Sugar ORM阻止UI线程初始化大约2.4秒。
有没有办法在后台线程中完成这些工作以改善用户体验?
04-07 20:14:42.179 10407-10407/com.sample.app I/Sugar﹕ on create
04-07 20:14:44.354 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.358 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.358 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.360 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.362 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.365 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.366 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.368 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.370 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.372 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.373 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.375 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.377 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.378 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.380 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.381 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.383 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.385 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.389 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.390 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.392 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.395 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.397 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.399 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.400 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.402 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.402 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.405 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.406 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.407 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.407 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.408 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.409 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.410 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.411 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.412 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.413 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.414 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.415 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.415 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.416 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.417 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.418 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.418 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.420 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.420 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.420 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.421 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.421 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.422 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.422 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.423 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.423 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.424 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.425 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.426 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.427 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.427 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.429 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.429 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.430 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.431 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.431 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.432 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.432 10407-10407/com.sample.app I/Sugar﹕ domain class
04-07 20:14:44.506 10407-10407/com.sample.app I/Sugar﹕ create table
04-07 20:14:44.506 10407-10407/com.sample.app D/Sugar﹕ Fetching properties
04-07 20:14:44.507 10407-10407/com.sample.app I/Sugar﹕ creating table ARTICLE
答案 0 :(得分:5)
正如您建议在后台任务中进行数据库初始化是我脑海中最简单的解决方案。 起初,我猜你已经注意到,当你使用扩展SugarRecord的对象时,第一次开始制糖,所以我所做的就是在应用程序启动的最初阶段(当然是在后台)拉动这个动作)。这就是我的方式:
首先我定义了一个像这样的对象:
public class DBInit extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
Offer.last(Offer.class);
return null;
}
}
Offer是我的自定义对象,扩展了SugarRecord。也就是说,通过执行虚拟搜索,我会触发数据库创建。
我在应用初始化中尽快启动此任务。例如在onCreate中,就像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Init the data base in a AsyncTask
DBInit dbInit = new DBInit();
dbInit.execute();
}
我希望您发现此解决方法非常有用。
编辑:如果你使用没有.last()方法的版本,你可以通过Offer.listAll(Offer.class)
代替Offer.last(Offer.class)
来实现同样的目标。但是,我会避免这种情况,因为你最终可能会将已存储的数百个或数千个对象拉到那里。所以我在我的项目中所做的是定义一个抽象类并使我的对象继承而不是来自SugarRecord,让我们看看它:
public abstract class BaseSugarRecord extends SugarRecord{
public static <T extends SugarRecord> T checkFor(Class<T> type, final String _id, int id){
List<T> found = find(type, _id+" = ?", String.valueOf(id));
return !found.isEmpty() ? found.get(0) : null;
}
public static <T extends SugarRecord> List<T> listAll(Class<T> type, final String _id, int id){
return find(type, _id+" = ?", String.valueOf(id));
}
}
参数final String _id
必须是用于执行搜索的变量的序列化名称。建议您在自定义类中定义它,如下所示。此外,int id
是此类变量的值,在本例中为自定义ID。
使用扩展SugarOrm的这个自定义类允许我重载.listAll
方法以及包含自定义方法。此外,我正在寻找的是搜索不返回单个对象的数据库,以节省内存消耗。然后,我通过更改之前定义的doInBackground中的函数来执行此操作:
@Override
protected Void doInBackground(Void... params) {
Offer.checkFor(Offer.class,Offer.ID_OFFER, 0);
return null;
}
在这里,我将如何使自定义对象继承自此抽象类:
public class Offer extends BaseSugarRecord {
public final static String ID_OFFER = "id_offer";
@SerializedName(ID_OFFER)
private int idOffer;
// ...