3

我在 1.3.1 版中为我的 android 5.1 应用程序使用 Sugar ORM。

今天,我在安装我的应用程序后注意到,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
4

1 回答 1

5

正如您建议的那样,在后台任务中进行数据库初始化是我想到的最简单的解决方案。一开始,我想你应该已经注意到,当你使用一个扩展 SugarRecord 的对象时,糖的创建第一次开始,所以我所做的就是在我的应用程序启动的一开始就拉这个动作(当然是在后台)。这就是我的做法:

  1. 首先我定义了一个像这样的对象:

    public class DBInit extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... params) {
            Offer.last(Offer.class);
            return null;
        }
    }
    

    Offer 是我的一个自定义对象,它扩展了 SugarRecord。也就是说,通过执行虚拟搜索,我触发了数据库创建。

  2. 我在应用程序初始化中尽快启动此任务。例如在 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)代替 .last() 方法来实现相同的效果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;
    // ...
于 2016-01-15T14:34:50.563 回答