Git Product home page Git Product logo

roomdemo's Introduction

Note

记得在build.gradle添加 apply plugin: 'kotlin-kapt'

    kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
    androidTestImplementation "androidx.room:room-testing:$room_version"

Tips

建表语句可以直接复制AppDatabase_Impl.java-> createAllTables的。需要注意的就是改变了表结构之后重新Build一下

Relations

Database relations with Room

注意Dao文件中@Transaction的使用

Many-to-Many

当两个类的PrimaryKey都叫idjoin表里又自定义了两个字段的id:如下:

      @Entity
      data class PersonSubjectJoin(
      
          @ColumnInfo(name = "p_id")
          val personId: Int,
          
          @ColumnInfo(name = "s_id")
          val subjectId: Int
      )

这个时候定义PersonWithSubjects或者 SubjectWithPerson类的时候需要指定associateByparentColumnentityColumn,像下边这样:

 data class PersonWithSubjects(
 
     @Embedded val person: Person,
 
     @Relation(
         parentColumn = "id",
         entityColumn = "id",
         associateBy = Junction(
             PersonSubjectJoin::class,
             parentColumn = "p_id", //note
             entityColumn = "s_id"  //note
         )
     )
     val subjects: List<Subject>
 )
  

Migration

Migration测试 https://medium.com/androiddevelopers/testing-room-migrations-be93cdb0d975

Migration测试官方Sample

TypeConverter

//官方Simple中的Converter例子
public class DateConverter {
    @TypeConverter
    public static Date toDate(Long timestamp) {
        return timestamp == null ? null : new Date(timestamp);
    }

    @TypeConverter
    public static Long toTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

文章

Understanding migrations with Room

理解Room的数据迁移

Room 🔗 Coroutines

[译] Room 🔗 Coroutines

Florina Muntenescu

roomdemo's People

Contributors

magicer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.