Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sugar ORM blocking UI Thread on init

I'm using Sugar ORM in Version 1.3.1 for my android 5.1 app.

Today, i noticed after installing my app that Sugar ORM is blocking the UI Thread for initialization for about 2.4 seconds.

Is there a way to get this stuff done in a background thread to improve the 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
like image 327
0xPixelfrost Avatar asked Apr 30 '26 01:04

0xPixelfrost


1 Answers

As you suggest doing the data base initialization in a background task is the simplest solution that comes to my mind. At first, I guess you noticed that the sugar creation starts for the first time just when you use an object that extends the SugarRecord. So what I do is pull this action in the very beginning of my app launch (in the background of course). And this is how I do it:

  1. First I've defined an object just like this:

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

    Where Offer is a custom object of mine that extends SugarRecord. That is, by performing a dummy search I trigger the DB creation.

  2. I launch this task as soon as possible in the app initialization. For instance in the onCreate, just like this:

     @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();
     }
    

I hope you find this workaround useful enough.

EDIT: If you use a version without .last() method you may achieve the same by doing Offer.listAll(Offer.class) instead of Offer.last(Offer.class). However, I would avoid that because you could end up pulling over hundred or thousands objects you have stored in there. So what I do in my projects is to define an abstract class and make my objects inherit from this and not from SugarRecord, let's see it:

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));
    }
}

The parameter final String _id must be the serialized name of the variable used to perform the search. It is recommended to define it inside your custom class as you'll see below. Also the int id is the value of such variable, in this case, a custom ID.

Using this custom class that extends SugarOrm allow me to overload the .listAll method as well as include custom ones. Furthermore, what I'm looking for is to perform a search into the DB that doesn't return a single object in order to save memory consumption. And I do that, then, by changing the function inside doInBackground defined before:

@Override
protected Void doInBackground(Void... params) {
    Offer.checkFor(Offer.class,Offer.ID_OFFER, 0);
    return null;
}

Here I put an extract of how I make my custom objects to inherit from this abstract class:

public class Offer extends BaseSugarRecord {

    public final static String ID_OFFER = "id_offer";
    @SerializedName(ID_OFFER)
    private int idOffer;
    // ...
like image 67
Anexon Avatar answered May 02 '26 16:05

Anexon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!