Comments (17)
in my case i add this to parcelable methode so we prevent the detached from DAO exception when you passe an object to a new Activity
protected Entity (Parcel in) {
this.daoSession = MainApplication.getInstance().getDaoSession();
......
}
from greendao.
+1 for this, would be awesome
from greendao.
I am actually exactly at the point where I would prefer to just pass the entity to another intent. Right now I am passing always the entity id and doing a query in the new Activity again. I guess that the DAO is doing some caching, thus this query should be quite quick. I heard that Serialization in Android is not very fast.
So what is finally the best way to do it?
from greendao.
@deradam: passing IDs is fine. greenDAO's load() tries to do a map lookup with the ID first (identity scope), so this should be very, very fast if the entity is available. This should be even faster than creating the parcel and making an object out of it again.
One thing to keep in mind: The identity scope holds only weak references to the entity, but usually the entities are referenced in the calling activity.
from greendao.
Any news about this feature?
from greendao.
I too would get great use out of this feature. I want to eliminate a DB hit when the device is rotated and my Fragment is recreated to improve latency. Since all the fields and types are declared, it seems it would be possible...
from greendao.
This feature would certainly improve the generation process.
from greendao.
Any news about this feature, is it even in consideration? At least I'd like to be able to remove the "private" modifier so I can implement @parcel, but until then - poor serialization....
from greendao.
Hello no news about parcelable?
from greendao.
You can add Parcelable support right now by using interfaces and keep sections. The downside is that the Parcelable code is not updated when the entity fields change, although the Parcelable code will be retained when you generate the entire schema again.
I would be interested in better parcelable support that automatically updates the parcelable code when the entity fields change.
Here is how to manually add Parcelable support:
- In your generator, enable keep sections on the schema:
schema.enableKeepSectionsByDefault(); - For each Parcelable entity in the schema above, add the following:
entity.implementsInterface("android.os.Parcelable"); - Run the generator to generate the files
- Go to each Parcelable entity file, find the KEEP METHODS section and add the Parcelable methods there.
// KEEP METHODS - put your custom methods here
@Override
public int describeContents()
{
return 0;
}
...
// KEEP METHODS END
Find the KEEP INCLUDES section and move all the custom imports there:
// KEEP INCLUDES - put your custom includes here
import android.os.Parcel;
...
// KEEP INCLUDES END
HINT: You can use a plugin that automatically writes the Parcelable boilerplate.
https://github.com/mcharmas/android-parcelable-intellij-plugin
from greendao.
I have modified our local branch to partially support this. It makes sense with dynamic to not have to use the KEEP blocks.
from greendao.
How to do it with greedDao gradle plugin?
- In your generator, enable keep sections on the schema:
schema.enableKeepSectionsByDefault(); - For each Parcelable entity in the schema above, add the following:
entity.implementsInterface("android.os.Parcelable");
from greendao.
@adjorno See my comment on #427 for an example. -ut
from greendao.
Thanks, but I have just realized that its illegal to make greeanDao entities Parcelable:
I don't want to reload entities from DB on device rotation because it takes some time, that is why I was going to put them in Bundle as Parcelable in onSaveInstanceState. Android tries to reuse Parcelables if its possible and I got the same ones and everything works fine in this case. But!.. There is another more tricky case - magic "Don't keep activities" button. This case is like device rotation but all the data should be restored from scratch. Restored greenDao entities will contain all the data but they wont have daoSession and app crashes with exception something like "Entity is not attached to DAO context".
I see 2 solutions:
- create real POJO entities and convert to them when I got result from DB request
- drop greenDao and work with raw sqlite requests.
Is there any ways to save & restore greenDao entities?
from greendao.
@adjorno Simple answer: don't. Just query for them again. greenDAO will hold onto entity instances in its session cache, so on config changes a query returns the same objects without having to re-create them.
Edit: Or use the Android Loader framework or something equivalent. -ut
from greendao.
I guess the caching doesn't work for my case, because I query raw sqlite request:
SELECT TRACK_ID, ARTIST_ID, TRACK.TITLE, ARTIST_ID, ARTIST.NAME,
SUM((LIST_SIZE + 1 - RANK) * (LIST_SIZE + 1 - RANK)) AS RATING FROM CHART_LIST
INNER JOIN CHART_TRACK ON CHART_LIST._ID = CHART_TRACK.CHART_LIST_ID
INNER JOIN CHART ON CHART._ID = CHART_LIST.CHART_ID
INNER JOIN TRACK ON TRACK._ID = CHART_TRACK.TRACK_ID
INNER JOIN ARTIST ON ARTIST._ID = TRACK.ARTIST_ID
WHERE CHART_ID = %theChartId% AND date(DATE) > date('%theFrom%') AND date(DATE) < date('%theTo%')
GROUP BY TRACK_ID ORDER BY RATING DESC
...
final Cursor theCursor = daoSession.getDatabase().rawQuery(theQuery, new String[0]);
final List theTracks = daoSession.getTrackDao().loadAllDeepFromCursor(theCursor);
theCursor.close();
...
I do it via raw request since I could not find any support of aggregate functions (SUM) and grouping (GROUP BY) in greenDao. On some slow devices it takes up to 10 seconds to handle this request (~200K tracks) and I don't want to do it again on config changes. I'd like to have a possibility to get these entities fast that is why I wanted to put them into the Bundle. Now I see that it is illegal with greenDao.
I am going to create some manual caching to put the result to separate "CacheTrack" table and try to re-use it on config changes firstly, and if there is nothing in it than do the real request I described above.
Is there a way to customize greenDao caching mechanism to support my case?
from greendao.
As said, this is exactly what the Android loader framework is for. Just write your own AsyncTaskLoader. -ut
from greendao.
Related Issues (20)
- Is there any plan for supporting gradle 7.0? HOT 2
- open software HOT 1
- incompatible gradle-6.5-bin.zip HOT 1
- Blocker issue in Sonarqube HOT 2
- Openharmony application HOT 1
- 一对多的关系表映射问题 HOT 3
- disk image is malformed (Sqlite code 11 SQLITE_CORRUPT) HOT 1
- How to do data filling, such as populating data from a database in assets into an app HOT 1
- Read this first! Check out our new database
- kotlin version problem HOT 1
- Can I use greenDao in multiple libraries? ------ Look forward to your reply HOT 1
- 什么时候支持协程 HOT 1
- Will a new version be released in the future when a bug occurs in the project? HOT 1
- DaoGenerator.getConfiguration Exception HOT 2
- Lambda 表达式报错 HOT 2
- The project encountered errors after updating Gradle Version 7.6.1 to Gradle Version 8.0+ HOT 40
- when use ’group by‘ ,the result is not expected HOT 1
- Unable to load class 'org.greenrobot.greendao.codemodifier.FormattingOptions'.
- DaoMaster class error HOT 1
- error: cannot find symbol db.execSQL("CREATE TABLE " + constraint + "\ symbol: method makeConcatWithConstants(Lookup,String,MethodType,String) location: interface StringConcatFactory 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 greendao.