Comments (8)
Hi!
I am under the impression that the use of POJOs is in fact an anti-pattern for Android, where resources (memory, and cpu) are constrained.
I think using POJOs means you're going to create collections of objects = garbage collection = cpu usage...
And I don't want this tool to encourage bad practices :)
from android-contentprovider-generator.
@BoD maybe when using with http://square.github.io/retrofit/ need those POJOs, then through POJOs to save data to database.
from android-contentprovider-generator.
There are times where cursors just don't cut it.
Let's say you have a these models:
Author
Book
You need to be able to show all books grouped by author.
One solution would of course to fetch all authors and then perform one query per author to fetch the books for that author but this isn't very efficient since the number of queries grows with the number of authors.
For me just being able to create a stupid POJO without any way to interact with the DB would be enough.
What I would love is something similar to this:
BookCursor cursor = new BookSelection().query(contentResolver);
cursor.moveToNext();
// This creates a new model from the cursor which copies all the fields
Book book = cursor.cloneModel();
book.setTitle("Some title");
// This creates content values from the model, ready to be put back in to the database
BookContentValues contentValues = book.getContentValues();
With the changes in 1.9 the need isn't as great since the cursors now implement the model interface but it would be nice to be able to give a model to other components without having to worry about things breaking when you move the cursor.
from android-contentprovider-generator.
I think your first use case ("show all books grouped by author") is more of a case for joins and group by (which are both already supported) than for POJOs.
Your second use case is interesting: if I understand correctly, you want to be able to create a ContentValues from one row of a Cursor - to easily update one (or a few) column(s) of one particular row.
I can see this could be useful, but I'm not sure this requires the generation of POJOs - there may be other ways.
If we're on the same page, can you open a different issue for this use case?
from android-contentprovider-generator.
Well, even with joins you cannot get a multi dimensional result set back since group by
only collapses rows that share some property into a single row.
What you could do as I mentioned is this:
AuthorCursor authorCursor = new AuthorSelection().query(contentResolver);
while (authorCursor.moveToNext()) {
BookCursor bookCursor = new BookSelection()
.authorId(authorCursor.getId())
.query(contentResolver);
// Do something with the books
}
But this would not scale well since the number of queries scales with the number of authors.
I don't see any other solution than to parse each row of the books into a POJO and put them in a map with the author ID as key.
But perhaps there is some other way that I've missed?
from android-contentprovider-generator.
In fact, you are right, group by
is probably useless in this case. I think this could simply be done by selecting all the books, joining the authors, and sort by author id. Then when iterating over the cursor, look at the author id, if it is different than the previous one, then it's a new "group".
from android-contentprovider-generator.
Sure, that works when I need to do something once with the books for each author but say I need to fetch all the books for author X, for example when using a list adapter.
You'll need to have fetched all books in the end but when showing a single item you'll need the books for just one author.
I totally agree that POJOs should be avoided in favor for cursors but sometimes you need a multi dimensional result.
But perhaps this use case isn't common enough to warrant this feature, after all it's not that big of a problem to parse this yourself.
Creating content values from a cursor isn't as attractive (to me at least) if it always contains all fields for a table since some columns might be read only (such as create time, id etc).
But if we had POJOs they could keep track of which fields that have changed and only generate content values for those fields like so:
BookCursor bookCursor = new BookSelection().id(1).query(contentResolver);
bookCursor.moveToNext();
Book book = bookCursor.toModel();
book.toContentValues(); // Empty BookContentValues or null
book.setTitle("Some other title");
book.toContentValues(); // BookContentValues containing only the title
book.setYear(2015);
book.toContentValues(); // BookContentValues containing both the title and the year
from android-contentprovider-generator.
Partially resolved in 1.11.0. You can do:
BookBean.copy(bookCursor)
Closing for now :)
from android-contentprovider-generator.
Related Issues (20)
- How add defaultValue:'' -- (empty) HOT 1
- how to update the value HOT 1
- Put // @formatter:off // @formatter:on around the whole generated code HOT 1
- Android Lint error HOT 3
- How use feature bulkInsert?
- Detect empty entity folder
- Could not find org.jraf:acpg-gradle-plugin
- What is the use of beans?
- FEATURE REQUEST: Notify multiple URIs for related tables
- Not qualified columns when projection is null (relevant again) HOT 3
- Method errors in generated code of v1.9.3 HOT 2
- Some support for simple cases of upgrades
- Adding support for CursorLoader HOT 6
- Notify option is not respected by *ContentValues.update method HOT 1
- How to use sum? HOT 3
- Support available for Batch insert HOT 3
- Add `offset` query parameter HOT 1
- selection.orderByX doesn't seem to work HOT 1
- To use the tool HOT 3
- Cannot declare a field "ID" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-contentprovider-generator.