我已经浏览了示例应用程序的文档和代码,并发现 files/objectbox/objectbox/data.mdb 是存储所有数据的默认文件。
假设我的理解是正确的,我有几个问题找不到文档:
我正在尝试使用对象库。
我阅读了官方文档并按照说明进行操作。但是仍然无法正常工作。
问题是当我尝试初始化boxStore对象时,找不到MyObjectBox类。
val boxStore = MyObjectBox.builder().androidContext(this).build()
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序模块。build.gradle(应用程序模块)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'
android {
compileSdkVersion 26
defaultConfig {
....
}
buildTypes {
release {
....
}
}
sourceSets {
main.java.srcDirs += 'src/main/java'
}
}
kapt {
generateStubs = true
arguments {
arg("objectbox.debug", true)
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
... other dependencies
//object box
implementation "io.objectbox:objectbox-android:$objectboxVersion"
// some useful Kotlin extension functions
implementation "io.objectbox:objectbox-kotlin:$objectboxVersion"
kapt …Run Code Online (Sandbox Code Playgroud) 我已在对象框中添加了多个实体并成功构建了 build_runner。\n flutter pun run build_runner build。\n现在,我添加了另外两个实体,但收到此错误 -无法解析注释 @Entity()
尝试添加实体
\nimport \'package:formula_application/objectbox.g.dart\';\nimport \'package:json_annotation/json_annotation.dart\';\n\npart \'stop_test_model.g.dart\';\n\n@JsonSerializable(\n explicitToJson: true,\n fieldRename: FieldRename.pascal,\n)\n@Entity()\nclass StopTest {\n StopTest(this.id, this.name);\n int id = 0;\n String name;\n //@Backlink()\n //final routes = ToMany<RouteTest>();\n\n factory StopTest.fromJson(Map<String, dynamic> json) =>\n _$StopTestFromJson(json);\n Map<String, dynamic> toJson() => _$StopTestToJson(this);\n}\n\n\\\\separate route test file\n\nimport \'package:formula_application/objectbox.g.dart\';\nimport \'package:json_annotation/json_annotation.dart\';\n\npart \'route_test_model.g.dart\';\n\n@JsonSerializable(\n explicitToJson: true,\n fieldRename: FieldRename.pascal,\n)\n@Entity()\nclass RouteTest {\n RouteTest(this.id, this.name);\n int id = 0;\n String name;\n //final stops = ToMany<StopTest>();\n\n factory RouteTest.fromJson(Map<String, dynamic> json) =>\n _$RouteTestFromJson(json);\n Map<String, dynamic> …Run Code Online (Sandbox Code Playgroud) 我正在使用 Android studio 4.1.1、ObjectBox 3.0.1 和 Kotlin 1.5.10。
当我点击测试类上的运行按钮或 run时,测试运行良好./gradlew :app-core:test,但是当我单击Run X with coverage测试运行时,我会看到
Project Is out of Date: Project class files are out of date. Would you like to recompile? The refusal to do it will result in incomplete coverage information
Run Code Online (Sandbox Code Playgroud)
如果我单击Cancel后台的覆盖率报告就会消失(因此我无法读取覆盖率信息,而且编辑器中的代码行左侧也没有覆盖率信息)。
如果我单击OK编译开始,我会在 ObjectBox 生成的文件中收到编译错误SomeEntityCursor:
Error:(14, 49) java: cannot find symbol
symbol: class SomeEntity
Run Code Online (Sandbox Code Playgroud)
其他生成的类中也会发生同样的情况(请参阅下面的详细信息)。但底线是生成的代码似乎无法看到我的实体类。值得注意的是:生成的代码与实体类位于同一模块中。
仅供参考,SomeEntity看起来就像这样:
@Entity
data class SomeEntity(@Id var id: Long = 0, …Run Code Online (Sandbox Code Playgroud) unit-testing code-coverage android-studio objectbox objectbox-android
我无法编写查询来获取 android 中 green dao 的 Objectbox 数据库中的不同值
是否有类似的东西:@Insert(onConflict = OnConflictStrategy.REPLACE) 在 Room 数据库中找到,您可以在 Objectbox 中执行。如果没有,如何避免在 objectbox 实体中保存重复条目?谢谢。
最近,我发现了有关Room db的文章,并且有第一个技巧将数据预填充到数据库中。
创建数据库时,当前是否存在某种优雅的方法来预填充数据?
我正在使用Dagger2,因此实际创建数据库非常容易。
@Module
class DatabaseModule{
@Provides
@Singleton
fun provideObjectBox(context: Context): BoxStore =
MyObjectBox.builder()
.androidContext(context)
.build()
}
Run Code Online (Sandbox Code Playgroud)
现在,我使用SharedPreferences的方式。因此,我只是检查它是否是数据库的第一个设置,而不是填充数据库。
JSON 是:
{
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
Run Code Online (Sandbox Code Playgroud)
我为User、Address、Geo、Company制作了四个实体类,以及这些实体的扩展UserModel、AddressModel、GeoModel、CompanyModel,其中每个模型都有 @JsonSerialized 注释来将 JSON 转换为模型。现在我们以地址为例。问题在于,当我必须将 AddressModel 传递给 User 实体类时 - 首先,我在 User 构造函数中没有地址字段,User 类中的第二个地址现在是最终地址 = …
为了表示货币值,我使用BigDecimal是因为其准确性(双精度类型会导致错误)。那么,如何将BigDecimal值存储在ObjectBox中,应该使用哪种类型的字段或转换器?
在对使用 ObjectBox 数据库的类进行单元测试时,我收到此错误:
io.objectbox.exception.DbDetachedException:无法解析分离实体的关系
我已将所有必要的数据实现到我的实体类中,以便在 macOS 上进行单元测试。因此,具有指向自身的一对一和一对多关系的实体关系。
@Entity
data class Category(
@Id var id: Long? = 0,
var title: String?,
@JvmField var parent: ToOne<Category>? = null,
@JvmField @Backlink(to = "parent") var subCategories: ToMany<Category>? = null){
constructor(): this(id = 0, title = "", parent = null, subCategories = null)
constructor(title: String): this(id = 0, title = title, parent = null, subCategories = null)
// normally transformer would add field, but need to manually for local unit tests
@JvmField @Transient var __boxStore: …Run Code Online (Sandbox Code Playgroud)