Room源码简析
一、源码架构
room-common、room-runtime、room-ktx三个依赖library
CRUD实质使用原生sqlite操作
room内生支持liveData&coroutines

源码概览
Room-Ktx扩展函数1. CoroutinesRoom.createFlow/execute//支持协程flow 数据流操作 //支持协程调度器 2. CoroutinesRoomKt:>>RoomDatabase.queryDispatcher 3. CoroutinesRoomKt:>>RoomDatabase.transactionDispatcher //事务扩展 4. RoomDatabaseKt:>> RoomDatabase.acquireTransactionThread() 5. RoomDatabaseKt:>> RoomDatabase.createTransactionContext() 6. RoomDatabaseKt:>> RoomDatabase.withTransaction //事务 TransactionElement
Room-common注解库//各种注解 标记 @update、@delete、@database、@entity、@room ......
Room-Runtime运行库//关注类 1.RoomDatabase //sqlite database操作helper 2.SupportSQLiteOpenHelper //crud的操作entity 3.EntityInsertionAdapter/EntityDeletionOrUpdateAdapter

二、流程原理
从使用开始
@entity、@dao、database------>>>通过注解kapt------>>生成
dao_impl、database_impl
创建数据库
Room.databaseBuilder-->RoomDatabase.builder-->FrameworkSQLiteOpenHelperFactory
database_impl---SupportSQLiteOpenHelper.create
创建表
`database_impl`----->> RoomOpenHelper.Delegate
操作表
dao_implentity------>>entityInsertionAdapter/EntityDeletionOrUpdateAdapter------>>在dao_impl中实现查询---->>
RoomSQLiteQuery------>>DbUtil---->>cursor查询
LiveData的支持:InvalidationTracker-->createLiveData-->InvalidationLiveDataContainer-->roomlivedata
三、附注
增删改默认不建议主线程操作(RoomDatabase.assertNotMainThread)增删改通过SharedSQLiteStatement实现类操作查通过SupportSQLiteQuery的实现类操作
版权声明:本文为opooooi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。