DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Loaders and cursors for DBFlow

Open hilljh82 opened this issue 8 years ago • 7 comments

This pull request contains the implementation of the following classes to improve Loader support for DBFlow.

  • FlowCursorAdapter: The purpose of this class is to improve support for CursorAdapter. In particular, we overload the getItem (position) method to return the actual model element at that position instead of a Cursor.
  • FlowSimpleCursorAdapter: This class allows the SimpleCursorAdapter to easily support DBFlow. Similar to FlowCursorAdapter, we overload the getItem(position) method to return a model element, instead of a Cursor.
  • FlowCursorLoader: This class integrates DBFLow with the Android Loader framework. In particular, the loader must be created with a Queriable object. The Queriable object is used by the loader to load models. The end result of this loader is a Cursor that can be used like the cursor returned by CursorLoader. A key feature of this loader is the ability to leverage the model observer framework in DBFlow via the (un)registerForContentChanges() methods. Using this methods will configure the loader to observe changes to models of interest. Any change observed will trigger the loader to automatically reload. This simplifies storing data in the database via DBFlow models, and updating corresponding views as a result of the storage updates.
  • FlowSingleModelLoader: This is the base class for the following single model classes: FlowModelLoader, FlowModelViewLoader, FlowModelQueryLoader. Single model loaders are for queries that return 1 model, not a list of models or a cursor to a list of models. We treat these different from the list loaders because we need to appropriate InstanceAdapter to create the single model, and load it from the cursor. The result of each load is a single, usable model. Similar to the FlowModelLoader, these loaders will automatically reload if the underlying table changes. Also, you can register for updates to models that have nothing to do with the final model. This is of interest when dealing with query models since the final model may be the composition of data from many different tables.

Lastly, each loader has been tested in applications that I am currently using. It would be great if these classes can be incorporated into the next release of DBFlow.

hilljh82 avatar Dec 01 '16 01:12 hilljh82

@agrosner Is there anything blocking this PR for being merged?

hilljh82 avatar Dec 06 '16 23:12 hilljh82

This PR is a key feature for me. @hilljh82 is not alone, I'm happy when this PR is merged soon

hannesa2 avatar Dec 18 '16 07:12 hannesa2

@hilljh82 please solve the conflict to let us (who wait for this feature) know, if @agrosner ever will merge this PR. Thank you !

hannesa2 avatar Mar 02 '17 13:03 hannesa2

@hilljh82 I am trying to use FlowCursorLoader class. It works but sometimes, I get

java.lang.IllegalStateException: Cannot execute task: the task is already running. at android.support.v4.content.ModernAsyncTask.executeOnExecutor(ModernAsyncTask.java:424) at android.support.v4.content.AsyncTaskLoader.executePendingTask(AsyncTaskLoader.java:219) at android.support.v4.content.AsyncTaskLoader.dispatchOnCancelled(AsyncTaskLoader.java:232) at android.support.v4.content.AsyncTaskLoader$LoadTask.onCancelled(AsyncTaskLoader.java:88) at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:474) at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:493) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5438) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

Googled a lot but no success. I am probably doing something wrong. I wonder, if you ever got this issue and/or know how to fix this?

Thanks !

zubinmehta avatar Mar 21 '17 10:03 zubinmehta

No, I have not. Do you mind sharing the code where you are creating the loader?

On Tue, Mar 21, 2017, 6:41 AM Zubin Mehta [email protected] wrote:

@hilljh82 https://github.com/hilljh82 I am trying to use FlowCursorLoader class. It works but sometimes, I get

java.lang.IllegalStateException: Cannot execute task: the task is already running. at android.support.v4.content.ModernAsyncTask.executeOnExecutor(ModernAsyncTask.java:424) at android.support.v4.content.AsyncTaskLoader.executePendingTask(AsyncTaskLoader.java:219) at android.support.v4.content.AsyncTaskLoader.dispatchOnCancelled(AsyncTaskLoader.java:232) at android.support.v4.content.AsyncTaskLoader$LoadTask.onCancelled(AsyncTaskLoader.java:88) at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:474) at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:493) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5438) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

I have been stuck at this issue, after googling a lot. I am probably doing something wrong. I wonder, if you ever got this issue and/or know how to fix this?

Thanks !

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Raizlabs/DBFlow/pull/1091#issuecomment-288039402, or mute the thread https://github.com/notifications/unsubscribe-auth/AB7C_QGHPrDHeGBWoU_rYDskdynf5yR9ks5rn6k9gaJpZM4LA5AA .

hilljh82 avatar Mar 21 '17 12:03 hilljh82

sigh I really hate to see this PR just go to waste.

@agrosner Did you make a final decision on declining this PR yet?

Harti avatar Oct 26 '17 15:10 Harti

@Harti Yes, it is unfortunate this PR has not been integrated into master—especially since I have twice addressed merge conflicts due to master changing only for my updates to become outdated once again.

If you are interested, I have create a separate project that provides these loaders:

https://github.com/onehilltech/backbone/tree/master/backbone-dbflow

Until this PR gets merged, I have been using this project. It is also a place that I use to implement support classes for DBFlow since the experience with this PR leaves me with little confidence some cool ideas I have will ever be integrated!

In addition, I have been working on another project called backbone-data that is designed to the equivalent of ember-data for Android. It uses DBFlow and Retrofit under the hood. It's a work in progress, but its making my life a LOT easier on Android since it codifies much if the redundant code that I was creating when using DBFlow.

hilljh82 avatar Oct 27 '17 00:10 hilljh82