Room 2.2.0-alpha01

Room 2.2.0-alpha01

E505897a79eede1a676f92740261e8f8?s=128

Masatoshi Kubode

August 01, 2019
Tweet

Transcript

  1. ©2019 Wantedly, Inc. 3PPNBMQIB 4IJCVZBBQL ٱอग़խढ़ 2019/08/01

  2. ©2019 Wantedly, Inc. ٱอग़խढ़ 8BOUFEMZ 8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ ,PUMJO .BHJDUIF(BUIFSJOH 1PLFNPO(P ࣗݾ঺հ

    5XJUUFS !TXJ[@BSE (JU)VC !LVCPEF
  3. ©2019 Wantedly, Inc. w 3PPNBMQIBͰ৽ػೳ͕৭ʑ૿͑ͨ w ৮ͬͯΈͨͷͰ঺հ͍ͨ͠ https://github.com/kubode/sandbox-room-2.2.0-alpha01 ࿩͢͜ͱ

  4. ©2019 Wantedly, Inc. 3PPNBMQIBͷ৽ػೳ

  5. ©2019 Wantedly, Inc.  1SFQBDLBHFE%BUBCBTF  4DIFNB%FGBVMU7BMVFT  .BOZUP.BOZ3FMBUJPOT 

    0OFUP0OF3FMBUJPOT  5BSHFU&OUJUZ  (SBEMF*ODSFNFOUBM"OOPUBUJPO1SPDFTTPS ৽ػೳ
  6. ©2019 Wantedly, Inc. 1SFQBDLBHFE%BUBCBTF

  7. ©2019 Wantedly, Inc. w BTTFUT΍ϑΝΠϧ͔Β৽͘͠%#Λ࡞ΕΔ w ༧ΊॳظσʔλΛ༻ҙͰ͖Δ આ໌

  8. ©2019 Wantedly, Inc. Code // createFromAsset/File can't use with inMemoryDb

    Room.databaseBuilder(…) .createFromAsset("assets-path-to.db") // OR .createFromFile(File("path-to.db")) .build() Room.inMemoryDatabaseBuilder ͸ϥϯλΠϜΤϥʔʹͳΔ
  9. ©2019 Wantedly, Inc. 4DIFNB%FGBVMU7BMVFT

  10. ©2019 Wantedly, Inc. w !$PMVNO*OGP EFGBVMU7BMVF  w ஋͕ͦͷ··$3&"5&5"#-&ʹ࢖ΘΕΔ આ໌

  11. ©2019 Wantedly, Inc. Code @Entity data class Memo( @PrimaryKey(autoGenerate =

    true) val id: Long, val text: String, @ColumnInfo(defaultValue = "('Created at ' || CURRENT_TIMESTAMP)") val createdAt: String ) @Dao interface MemoDao { @Query("INSERT INTO Memo (text) VALUES (:text)") fun insertNewRecord(text: String) } ੑ্࣭@Insertʹ͸࢖͑ͳ͍
  12. ©2019 Wantedly, Inc. .BOZUP.BOZ3FMBUJPOT

  13. ©2019 Wantedly, Inc. w !3FMBUJPO͸0OFUP.BOZʹ͔͠࢖͑ͳ͔ͬͨ w BTTPDJBUF#ZͰ.BOZUP.BOZ΋Մೳʹ આ໌

  14. ©2019 Wantedly, Inc. Code @Entity data class Playlist( @PrimaryKey(autoGenerate =

    true) val playlistId: Long, val title: String ) @Entity data class Song( @PrimaryKey(autoGenerate = true) val songId: Long, val title: String ) @Entity(primaryKeys = ["playlistId", "songId"]) data class PlaylistSongRef( val playlistId: Long, val songId: Long )
  15. ©2019 Wantedly, Inc. Code data class PlaylistWithSongs( @Embedded val playlist:

    Playlist, @Relation( parentColumn = "playlistId", entity = Song::class, entityColumn = "songId", associateBy = Junction(PlaylistSongRef::class) ) val songs: List<Song> ) @Dao interface MusicDao { @Transaction @Query("""SELECT * FROM Playlist""") fun loadAllPlaylistWithSongs(): List<PlaylistWithSongs> } ORDER BY͸ࢦఆͰ͖ͳ͍
  16. ©2019 Wantedly, Inc. 0OFUP0OF3FMBUJPOT

  17. ©2019 Wantedly, Inc. w !3FMBUJPO͸-JTU4FUʹ͔͠࢖͑ͳ͔ͬͨ w ୯ҰΦϒδΣΫτʹ΋࢖͑ΔΑ͏ʹͳͬͨ આ໌

  18. ©2019 Wantedly, Inc. Code @Entity data class User( @PrimaryKey val

    userId: Long, val name: String ) @Entity data class Profile( @PrimaryKey val userId: Long, val selfIntroduction: String )
  19. ©2019 Wantedly, Inc. Code data class UserWithProfile( @Embedded val user:

    User, @Relation( parentColumn = "userId", entity = Profile::class, entityColumn = "userId" ) val profile: Profile? ) @Dao interface UserDao { @Transaction @Query("""SELECT * FROM User""") fun loadAllUserWithProfile(): List<UserWithProfile> } Nullableʹ͢Ε͹1:0..1΋දݱͰ͖Δ
  20. ©2019 Wantedly, Inc. 5BSHFU&OUJUZ

  21. ©2019 Wantedly, Inc. w *OTFSU6QEBUF%FMFUFʹFOUJUZ͕௥Ճ͞Εͨ w ผͷΦϒδΣΫτΛ&OUJUZͱͯ͠อଘͰ͖Δ આ໌

  22. ©2019 Wantedly, Inc. Code @Entity data class Project( @PrimaryKey val

    id: Long, val title: String, @ColumnInfo(defaultValue = "''") val longDescription: String ) data class ProjectMiniApiEntity( var id: Long, var title: String ) PartialʹͳΔColumn͸ Nullable͔defaultValueඞਢ
  23. ©2019 Wantedly, Inc. Code @Dao interface ProjectDao { @Insert(entity =

    Project::class) fun insertNewProject(projectMini: ProjectMiniApiEntity) @Update(entity = Project::class) fun updateProject(projectMini: ProjectMiniApiEntity) } INSERT OR ABORT INTO `Project` (`id`,`title`) VALUES (?,?) UPDATE OR ABORT `Project` SET `id` = ?,`title` = ? WHERE `id` = ?
  24. ©2019 Wantedly, Inc. Code class CharSequenceConverter { @TypeConverter fun toString(value:

    CharSequence?): String? = value?.toString() } data class ProjectMiniApiEntity( var id: Long, @ColumnInfo(name = "title") var title2: CharSequence, @Ignore var list: List<String> ) TypeConverter͕͋Ε͹ม׵ͯ͘͠ΕΔ @ColumnInfoͰ໊લͷม׵΋Ͱ͖Δ Ignore΋Ͱ͖Δ
  25. ©2019 Wantedly, Inc. w "1*Ωϟογϡ໨తͰ3PPNΛ࢖͍ͬͯΔͳΒศར w ҰཡͱৄࡉͰ৘ใྔ͕ҧ͏৔߹ͳͲ w !6QEBUFʹΑΔ෦෼Ξοϓσʔτ w

    ໊લ΍ܕ΋ม׵Ͱ͖Δ ॴײ
  26. ©2019 Wantedly, Inc. *ODSFNFOUBM͸ࢼͯ͠·ͤΜ

  27. ©2019 Wantedly, Inc. ·ͱΊ

  28. ©2019 Wantedly, Inc. w 5BSHFU&OUJUZΊͪΌͪ͘Όཉ͔ͬͨ͠΍ͭ w 3PPN͸ͲΜͲΜศརʹ࢖͍΍͘͢ w Ҿ͖ଓ͖3FMFBTFOPUFT؂ࢹ͍͖͍ͯͨ͠ ·ͱΊ