konmik / nucleus

Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
MIT License
1.98k stars 253 forks source link

[Q] How to work with ContentProvider? #119

Closed hnvn closed 7 years ago

hnvn commented 7 years ago

I'm a RxJava newbie. In my opinion, Retrofit, RxJava and this library is the best practice to implement a client-server communication in Android. Retrofit and RxJava makes easily to call and handle (asynchronous) HTTP requests, Nucleus makes safety to implement the callback in the life circle of Activity or Fragment. Data could come from either remote server or local data base. In remote server side, I have an awesome Retrofit library. In local data base, I could use SQLite Database and ContentProvider. I haven't found the best way to make an asynchronous query to ContentProvider that use RxJava and implement callback with Nucleus yet. Would you mind suggesting me?

konmik commented 7 years ago

There is no reason to use ContentProvider for a local database. I recommend using SQLBrite (or RxQuery as a simpler example).

TWiStErRob commented 7 years ago

What if you want to publish you database as a content provider? Since you already have that API (the CP) you might as well use it, or even then it's worth having two data accesses (internal/published) to the same source?

konmik commented 7 years ago

Premature optimization, premature abstraction and premature implementation are all the roots of the same evil. KISS is the only cure.

I would probably implement ContentProvider on top of SQLBrite. But I'm not sure, it depends. The core must be as simple as possible. ContentProvider is a terrific complication.

TWiStErRob commented 7 years ago

Ah, yes, true that, just need to reverse the dependency; thanks.

hnvn commented 7 years ago

My research drives me to a great article: https://medium.com/@Viraj.Tank/realm-integration-in-android-best-practices-449919d25f2f#.u8biwjtdg Reative Programing in Android = RxJava + Retrofit + Realm (y)

konmik commented 7 years ago

Realm? Oh, shi... :D I will not give any advices here.

hnvn commented 7 years ago

Why do you dislike Realm? I'm trying refactoring my codebase and I really confuse about many new technologies. Would you mind sharing some your experiences about them?

konmik commented 7 years ago

no multithreading support too big executable size no sqlite compatibility no null support no immutable objects support database browser is for mac only And the most important - it makes you ditch your architecture for the sake of an IO device. Database is just an IO device, one of many. It should not take central place in the app architecture.

konmik commented 7 years ago

Here is a bit more discussion, if you're interested https://m.reddit.com/r/androiddev/comments/3fcd1s/is_it_bad_practice_to_convert_a_cursor_once/ctorbrn

I'm JackHexen there.

TWiStErRob commented 7 years ago

SQLite compatibility is understandable as it's a DB replacement, not an ORM framework, but I agree with all the points. It would be interesting to review what changes they made and how they delivered on the promises of Emanuele's comment on reddit.

hnvn commented 7 years ago

Your advices is very useful. But I really like Realm in a its feature: "LIVE object". Live object is similar with reactive programming, Realm object will callback when it changes. It's really cool when I build a app that need a cache architect. I will wire up my View object with Realm object to update my data and show it in my UI. Then, I will pull data from my server by using Retrofit, save it in Realm, and Realm will auto update my UI. If I don't have a Internet connection, I use cache data in Realm. If I have connection, I have new data in Realm. I don't need to switch my code to handle these cases. In my opinion, It's really straightforward.