天天看點

greenDAO 使用

greenDAO Documentation https://greenrobot.org/greendao/documentation/

Tutorials and How-Tos

How to get started

Introduction – project setup

Modelling entities – schema and annotations

Sessions – identity scope and session cache

Notice that the database connection belongs to the DaoMaster, so multiple sessions refer to the same database connection. Hence, new sessions can be created quite quickly. However, each session allocates memory, typically a session “cache” for entities.

Identity scope and session “cache”

If you have two queries returning the same database object, how many Java objects are you working with: one or two? It depends entirely on the identity scope.

The default in greenDAO (the behavior is configurable) is that multiple queries return references to the same Java objects. For example, loading a User object from the USER table with ID 42 returns the same Java object for both queries.

A side effect of this is some kind of entity “caching”. If an entity object is still around in memory (greenDAO uses weak references here), the entity is not constructed again. Also, greenDAO performs no database query to update the entity values. Instead, the object is returned “immediately” from the session cache, which is one or two magnitudes faster.

Queries – using the query builder

Joins – multi-table joins and self-joins

Relations – to-one and to-many relations between entities

Custom types – mapping classes and enums to database values

Database Encryption – using SQLCipher 

GitHub greenDAO 

繼續閱讀