Android架构组件—Room 编译遇到的坑

 

编译错误

解 : Entuty 加 getter & setter

Schema 错误提示

Warning:  //此时build是成功的。
Error:(13, 17) 警告: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
 
解 [ref]https://stackoverflow.com/questions/44322178/room-schema-export-directory-is-not-provided-to-the-annotation-processor-so-we 
//官网文档也有写的。

方案A exportSachma = false;

@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
   ...
}
 
方案B //看看大神的回答, 不说答案正确与否, 就这种代码注释值得学习。

----->>project-->>app--->>schemas---->>xxx.很长名字后面最后一个Database---->>会产生一个1.json

android {

// ... (compileSdkVersion, buildToolsVersion, etc)

defaultConfig {

    // ... (applicationId, miSdkVersion, etc)

    javaCompileOptions {
        annotationProcessorOptions {
            arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
        }
    }

}

// ... (buildTypes, compileOptions, etc)
 
}

生成的Schema //关于Schema ,看官网

 
Schema JSON

    {
  "formatVersion": 1,
  "database": {
    "version": 1,
    "identityHash": "559f8304933f593d9feffa68b452449b",
    "entities": [
      {
        "tableName": "User",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER, `first_name` TEXT, `last_name` TEXT, PRIMARY KEY(`uid`))",
        "fields": [
          {
            "fieldPath": "uid",
            "columnName": "uid",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "firstName",
            "columnName": "first_name",
            "affinity": "TEXT"
          },
          {
            "fieldPath": "lastName",
            "columnName": "last_name",
            "affinity": "TEXT"
          }
        ],
        "primaryKey": {
          "columnNames": [
            "uid"
          ],
          "autoGenerate": false
        },
        "indices": [],
        "foreignKeys": []
      }
    ],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"559f8304933f593d9feffa68b452449b\")"
    ]
  }
}
 
/data/data/databases 目录下没有数据库文件

解 : 此时还没有数据 insert ,所以没出来。 insert后会出现。

Debugging 模式卡住

解: 此时删除schema 文件就能正常Debugging //不知到原因。

数据写入不了 //看前边的代码 – 感谢 monroe ,北京-秦微明 @Android studio QQ 群 的提点。

解 : 因为 transaction 完成需要commit,标记事务成功 setTransactionSuccessful()

     //这代码不能正常  insert why?
     mDb.beginTransaction();
     mDb.userDao().insertA(user);
     mDb.endTransaction();
 
src //非常粗糙的代码

github
--------------------- 
作者:AndroLi 
来源:CSDN 
原文:https://blog.csdn.net/zkwsr/article/details/76578312?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值