agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.88k stars 599 forks source link

Exception on ContentProvider query #662

Closed gorodechnyj closed 8 years ago

gorodechnyj commented 8 years ago

If I generate ContentProvider with DBFlow, and query model with CursorLoader like this:

return new CursorLoader(this, CustomModel.CONTENT_URI, null, null, null, CustomModel_Table._id + " DESC");

I get exception

java.lang.NullPointerException: Attempt to get length of null array at com.raizlabs.android.dbflow.runtime.BaseContentProvider.toProperties(BaseContentProvider.java:57) at ch.dotpay.db.DotpayDatabase_Provider.query(DotpayDatabase_Provider.java:136)

The exception is caused by reading length property of null projection field. But in Android SDK it is valid to specify null projection - it only indicates that CursorLoader should query all fields.

sp958857 commented 8 years ago

I have the problom

sp958857 commented 8 years ago

By the way, the "selection and selectionargs" seems no work in cursorLoader.

gorodechnyj commented 8 years ago

@sp958857 @agrosner I have created custom CursorLoader which works with current version of DBFlow, and also gets notified of content Uri changes. https://gist.github.com/gorodechnyj/cd357d1479111697637e

One can use it like this:

public class UserStatusListLoader extends CursorLoader {

    public UserStatusListLoader(Context context) {
        super(context,
                UserStatus.CONTENT_URI,
                SQLite.select().from(UserStatus.class).orderBy(UserStatus_Table.count_used, false));
    }
}

and later just return new UserStatusListLoader(YourActivity.this); from onCreateLoader method.

This class is just a copy of Android's CursorLoader class, adapted to DBFlow Query. Also earlier version of this code was introduced in @jiqimaogou pull request (https://github.com/Raizlabs/DBFlow/pull/135) but was never merged.

Personally I think this is the way we should use loaders with DBFlow.

gorodechnyj commented 8 years ago

@agrosner, I think you should make a special wiki page about how DBFlow can and should be used with CursorLoaders. And how Model instances could be restored from Cursor objects.

agrosner commented 8 years ago

in develop it now directly just calls down to native methods ...no conversions to worry about.