Share
Embed
How to select ORM?
@hotchemi
April 29, 2015
Transcript
34 slides
-
Agenda 1. Who am I? 2. How to select ORM?
3. ActiveAndroid 4. DBFlow 5. Conclusion -
Who am I? • Name: Shintaro Katafuchi • GitHub, Twitter:
@hotchemi • Library: Android-Rate https:/github.com/hotchemi/Android-Rate • From Japan, just sightseeing! • Member of DroidKaigi -
• Held in Tokyo 025/04/2015 • Over 400 participants •
22 session • http://droidkaigi.github.io • We’re planning Droidcon Tokyo… DroidKaigi -
How to select ORM? 1. Easy to use 2. Kind
documentation and support 3. Well known or not 4. Active development 5. Good performance -
The good parts • Pretty popular(★2383 in GitHub) • Quite
easy to use • Enough documentation and information • Using with ContentProvider -
@Table(name = "Items") public class Item extends Model { @Column(name
= "Name") public String name; @Column(name = "Category") public Category category; } Model -
Save and delete Category restaurants = new Category(); restaurants.name =
"Restaurants"; restaurants.save(); restaurants.delete(); -
The bad parts • Migration isn’t type safe • Difficulty
with complex join • Using a lot of reflection • The performance is not so high • Development isn’t active… • Because of Ollie? It isn’t active neither :) -
Complex join final From query = new Select("Genres.genre_id", "Genres.name as
genreName", "Genres.category_id", "Categories.name as categoryName") .from(Genre.class) .innerJoin(Category.class) .on("Genres.category_id = Categories.category_id") .where("Genres.active = 1 and Categories.active = 1") .orderBy("Categories.sort, Genres.sort"); final Cursor cursor = Cache.openDatabase().rawQuery(query.toSql(), query.getArguments()); -
Migration ALTER TABLE Items ADD COLUMN color INTEGER; ALTER TABLE
Items ADD COLUMN name TEXT; ALTER TABLE Items ADD COLUMN kind TEXT; BTTFUTNJHSBUJPOT/FX7FSTJPOTRM -
The good parts • Enough popular(★761 in GitHub) • Fullset
API(ModelView, observable, multiple database) • Enough documentation • Active development • The performance is higher than ActiveAndroid • Using APT -
Initialize @Database(name = AppDatabase.NAME, version = AppDatabase.VERSION) public class AppDatabase
{ public static final String NAME = "App"; public static final int VERSION = 1; } @Table(databaseName = AppDatabase.NAME) public class TestModel1 extends BaseModel { @Column(columnType = Column.PRIMARY_KEY) public String name; } -
Category restaurants = new Category(); restaurants.name = "Restaurants"; restaurants.save(false); //
async or not restaurants.delete(false); Save and delete -
ModelView @ModelView(query = "SELECT time from AModel where time >
0", databaseName = AppDatabase.NAME) public class AModelView extends BaseModelView<AModel> { @Column long time; } -
Migration @Migration(version = 2, databaseName = TestDatabase.NAME) public class Migration1
extends BaseMigration { @Override public void onPreMigrate() { // called before migration, instantiate any migration query here } @Override public void migrate(SQLiteDatabase database) { // call your migration query } @Override public void onPostMigrate() { // release migration resources here } } -
Observable FlowContentObserver.ModelChangeListener modelChangeListener = new FlowContentObserver.ModelChangeListener() { @Override public void
onModelChanged() { // called in SDK<14 } @Override public void onModelSaved() { } @Override public void onModelDeleted() { } @Override public void onModelInserted() { } @Override public void onModelUpdated() { } }; -
Observable observer.addModelChangeListener(modelChangeListener); TestNotifiableModel testModel = new TestNotifiableModel(); testModel.setName("myName"); // will
notify our observer automatically, no configuration needed! testModel.insert(async); testModel.update(async); testModel.save(async); testModel.delete(async); // when done with listener observer.removeModelChangeListener(modelChangeListener); -
The bad parts • Too early? • Too many functions
• Content Provider Generation • Triggers, Indexes • Model Containers • Other than that…a lot! -
Comparison ActiveAndroid DBFlow Easy ̋ △ Popular ◎ ̋ Active
development △ ̋ Documentation ̋ ̋ Perfomance ̋ ◎ Migration ☓ ̋ Function △ ◎ Simple ◎ ̋ -
Conclusion • If you want to use a simple API,
• →ActiveAndroid • If you want to see other’s sample or blog, • →ActiveAndroid • If you have to take care of performance, • →DBFlow • If you have to use multiple database, • →DBFlow -
Other than that? • ORMLite…Looks good to me but not
simple. • GreenDAO…Performance is awesome but it’s annoying to write dao generation code. • SugarORM…It seems have good performance than AA but…not popular. • Realm…The highest performance, but not SqLite, bad migration, we can only write setter/getter to Model. • Other than that I’m not sure…