2015-04-07 154 views
3

我在我的android 5.1應用程序中使用版本1.3.1中的糖ORM。初始化糖ORM阻塞UI線程

今天,我注意到安裝我的應用程序後,糖ORM阻止約2.4秒的初始化UI線程。

有沒有辦法在後臺線程中完成這些東西來改進UX?

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

似乎成爲一個功能:https://github.com/satyan/sugar/issues/269#issuecomment-90805581。任何解決方法是受歡迎的... –

+0

沒有解決方法,沒有錯誤修復 - >我切換到ActiveAndroid:https://github.com/pardom/ActiveAndroid –

回答

4

正如你所建議的,在後臺任務中做數據庫初始化是我想到的最簡單的解決方案。 起初,我想你應該注意到,當你使用延伸SugarRecord的對象時,糖的創造第一次開始,所以我做的是在我的應用程序啓動的初始階段(在課程)。這就是我要做的事:

  1. 首先我定義的對象,就像這樣:

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

    報價在哪裏是我的自定義對象擴展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)而不是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; 
    // ... 
+1

'.last' API不可用。我正在使用糖1.3 –

+1

@AdiyatMubarak:他的意思是使用任何方法來觸發SugarORM。最好不要使用listAll(),因爲它可能被證明是一個繁重的操作。 – BlueLeaf